[英]TS2339: Property 'sort' does not exist on type 'BehaviorSubject<AbstractControl[]
I am looking to create a dynamic table using angular material where i can add/remove data dynamically.我正在寻找使用角度材料创建一个动态表,我可以在其中动态添加/删除数据。 Being very new to Angular , i have referred follwing stackblitz to create the table.
作为 Angular 的新手,我参考了以下 stackblitz 来创建表格。
https://stackblitz.com/edit/angular-material-editable-table-fazhbc https://stackblitz.com/edit/angular-material-editable-table-fazhbc
While all my changes work - i am not able to sort the data at虽然我的所有更改都有效 - 我无法对数据进行排序
ngAfterViewInit() {
this.dataSource.sort = this.matSort;
}
The error i get TS2339: Property 'sort' does not exist on type 'BehaviorSubject<AbstractControl[]
我得到的错误 TS2339: 类型
'BehaviorSubject<AbstractControl[]
上不存在属性 'sort'
is the sorting isn't supported like in material table data source or am i missing something ?是不是像材料表数据源那样不支持排序,还是我遗漏了什么?
您需要使用value
或getValue()
从 BehaviorSubject 获取当前值,并按如下方式对其进行排序: this.dataSource.value.sort(this.matSort)
。
由于 BehaviorSubject 返回 observable 因此首先订阅该数据然后应用排序功能
You can think of this as a news reporter talking on TV and giving latest updates to all similarly Behaviour Subject pass latest data.您可以将其视为新闻记者在电视上交谈并为所有类似的行为主题传递最新数据提供最新更新。
Add one service as myService and add following code public myData: BehaviorSubject = new BehaviorSubject([]);添加一项服务作为 myService 并添加以下代码 public myData: BehaviorSubject = new BehaviorSubject([]); updateData(data): void { this.myData.next(data);
updateData(data): void { this.myData.next(data); }
}
then call updateDatafrom where you want to send data to others.然后从您想要向其他人发送数据的地方调用 updateData。 this.myService.updateData("Test");
this.myService.updateData("测试"); You can pass any type of data here like object,array etc. Then on other component where you want this data write following code this.myService.myData.subscribe((data) => { console.log(data); });
您可以在此处传递任何类型的数据,如对象、数组等。然后在您希望此数据的其他组件上写入以下代码 this.myService.myData.subscribe((data) => { console.log(data); }); You will get data.
你会得到数据。 You can write above in any component where you want data.
您可以在需要数据的任何组件中编写上面的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.