angular - Angular - 从数组更改中刷新垫表
问题描述
我有一个应用调用观察器,由一个组件/服务组成。我有一个简单的表格,可以正确显示为空
<table mat-table [dataSource]="this.getWatcherTable()" class="mat-elevation-z8">
... my columns and rows are described here
</table>
源链接到我的组件文件
export class WatcherComponent{
constructor(public watcherService : WatcherService ) {}
getWatcherTable(){
return this.watcherService.watcherTable;
}
}
本身链接到服务文件
export class WatcherService {
watcherTable : Array<Watcher> = new Array();
updateWatchers(name,value){
this.watcherTable.push({name:name, value:value});
}
}
更新由另一个应用程序触发并且运行良好,数组的控制台日志有更新,但数组仍然显示为空
解决方案
每次更新数组时,都应该创建一个新数组,以便 Angular 更改检测会自动更新组件。所以与其:
export class WatcherService {
watcherTable : Array<Watcher> = new Array();
updateWatchers(name,value){
this.watcherTable.push({name:name, value:value});
}
}
你应该有:
export class WatcherService {
watcherTable : Array<Watcher> = new Array();
updateWatchers(name,value){
this.watcherTable = this.watcherTable.concat([{name:name, value:value}]);
}
}
推荐阅读
- database - Array handling in mongodb
- r - 显示每个方面而不是整体的摘要行
- tizen - 无法在 Tizen Studio 中下载新图像和创建模拟器
- c# - (&(objectClass=user)(|(displayName)) 搜索过滤器无效
- r - 如何使用 R 中的 WOE 和 IV 替换分类或连续变量中的 NA?
- c++ - Spirit 仅在似乎从 Lexer 获得第一个符号后无法解析
- java - 如何从 Android 更新 DynamoDB 中的项目?
- hadoop - 如何在启用 kerberos 的 HDFS 中创建非超级用户
- c++ - clang-7:错误:链接器命令失败,退出代码为 1
- sql - 如何在其他列的基础上填充一列?