angular - 为什么模板没有改变?- 角
问题描述
模板是:
<tr *ngFor="let row of rows; let i = index">
<div (click)=""edit(row)></div>
</tr>
组件是:
public edit(row: PostAddress): void {
dialogRef.afterClosed().subscribe((postaddress) => {
if (postaddress) {
row = postaddress;
this._change.markForCheck();
}
}
我看到该变量已更改为新值: row = postaddress;
但模板未呈现。
我用 changeDetection: ChangeDetectionStrategy.OnPush
我也试过这个:
row = postaddress;
this._change.markForCheck();
this._change.detectChanges();
解决方案
您可以尝试发送索引并修改父数组中的元素,而不是重新分配数组中的元素。尝试以下
模板
<tr *ngFor="let row of rows; let i = index">
<div (click)="edit(index)></div>
</tr>
控制器
public edit(index): void {
dialogRef.afterClosed().subscribe((postaddress) => {
if (postaddress) {
this.rows[i] = postaddress; // <-- change item in the array
this._change.markForCheck();
}
});
}
推荐阅读
- vue.js - 为什么模板文字不输入新行?
- sql - PostgreSQL:查询 JSON 数组中的元素
- python - python - 如何迭代tensorflow中每个嵌套张量的第一个元素,python?
- javascript - AG Grid - 如果没有其他列被排序,则恢复默认排序
- json - Kotlin Moshi 从资产中加载 Json
- scala - 如何在 Scala 中应用重载方法 Using Picture?
- html - 下拉列表隐藏在弹出窗口后面
- python - 我应该在哪里实现烧瓶自定义命令(cli)
- xaml - Xamarin 窗体圆形按钮
- node.js - 如何使用 NODE JS 肥皂查看 ARRAY