angular - UserDataSource 在 Angular 中没有数据属性
问题描述
我正在尝试按照本指南将多选添加到 Angular Material 表中。它data
在 masterToggle 中引用,但我的 UserDataSource 中没有该属性。解决此问题的正确方法是什么?
我的组件
dataSource: UserDataSource;
ngOnInit() {
this.dataSource = new UserDataSource(this.svc);
this.dataSource.loadCompanies(this.lgid);
}
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
用户数据来源:
export class UserDataSource implements DataSource<Company> {
private CompanyModelsSubject = new BehaviorSubject<Company[]>([]);
private loadingSubject = new BehaviorSubject<boolean>(false);
public loading$ = this.loadingSubject.asObservable();
constructor(private svc: GreencardService) { }
connect(collectionViewer: CollectionViewer): Observable<Company[]> {
return this.CompanyModelsSubject.asObservable();
}
disconnect(collectionViewer: CollectionViewer): void {
this.CompanyModelsSubject.complete();
this.loadingSubject.complete();
}
loadCompanies(lgid)
{
this.loadingSubject.next(true);
this.svc.getCompanies(lgid).pipe(
catchError(() => of([])),
finalize(() => this.loadingSubject.next(false))
).subscribe(x => { this.CompanyModelsSubject.next(x);});
}
解决方案
我相信您正在遵循的示例是使用MatTableDataSource
具有公共吸气剂的类data
...
get data() { return this._data.value; }
而私有变量_data
是BehaviorSubject
带有初始化数据的。
constructor(initialData: T[] = []) {
super();
this._data = new BehaviorSubject<T[]>(initialData);
this._updateChangeSubscription();
}
因为您创建了自己的类,所以您需要实现一个类似的公共 getter... 可能如下所示。
get data() {return this.CompanyModelsSubject.value; }
推荐阅读
- android - Expo android createAlbumAsync 不起作用
- c++ - 时钟程序超出限制
- c# - 使用用户上下文/用户令牌 C# 调用 Microsoft Graph API
- python - 如何修改 Range 函数中边界值的包含/排除行为?
- java - 在这个例子中,spring 框架是如何知道如何实例化 RestHighLevelClient 的呢?
- reactjs - 使用生产构建处理使用 React 应用程序的电子邮件验证
- java - 检票口中的复杂模型
- autohotkey - AutoHotKey 不阻止输入
- java - 当类中有多个构造函数时,每次创建对象时执行代码
- javascript - 如何找出浏览器如何对输入地址栏中的 URI 进行编码