【发布时间】:2018-03-08 12:20:37
【问题描述】:
我正在使用由普通数组支持的Angular Material Table 作为数据源。
this.employees = this.route.snapshot.data.employes; // of type Employee[] resolved using a Resolve guard
this.dataSource = new MatTableDataSource<Employee>(this.employees);
最初呈现后,我想通过使用组件中的方法修改“this.employess”数组来从数据表中添加/删除行:-
addEmployee(e: Employee){
this.employess.push(e); // I expect the table to have one row added after this.
}
removeEmployee(index : number){
// splice the array at given index & remove one row from data table
}
问题
当我在数组中添加删除元素时,数据表行不受影响。 我发现了一个blog 阐述了同样的问题,但使用了自定义数据源。有没有办法使用普通数组?
【问题讨论】:
-
Angular 不会检测对象(包括数组)上的更改,因为数组引用在添加或删除元素时不会更改。