Html 用于在数据更改后不更新
我在angular中有一个Thin应用程序,在我的一个组件中,我通过父组件的Html 用于在数据更改后不更新,html,angular,ngfor,Html,Angular,Ngfor,我在angular中有一个Thin应用程序,在我的一个组件中,我通过父组件的@Input()列获取一些数据 我在视图中用一个普通的*ngFor来迭代它 let column of columns 但是当我更改数据时,我的*ngFor不会更改。我已经检查了ngochanges(changes:SimpleChanges)中的更改,并且我已经尝试重新分配值,我使用了lodash并使用了changedtectorref import _ from 'lodash'; constructor( pri
@Input()列
获取一些数据
我在视图中用一个普通的*ngFor
来迭代它
let column of columns
但是当我更改数据时,我的*ngFor
不会更改。我已经检查了ngochanges(changes:SimpleChanges)
中的更改,并且我已经尝试重新分配值,我使用了lodash
并使用了changedtectorref
import _ from 'lodash';
constructor( private cdr: ChangeDetectorRef){}
ngOnChanges(changes: SimpleChanges){
debugger; // this debugger does enter
this.columns = changes.columns.currentValue; ////didn't work
this.columns = [...this.columns]; ////didn't work
this.columns = _.cloneDeep(this.columns); ////didn't work
this.columns = [..._.cloneDeep(this.columns)]; ////didn't work
this.cdr.detectChanges(); ////didn't work
}
无论我尝试了什么,我的观点在*ngFor
中都保持不变,在它之前,我放了一个{{colums | json}}
,看看我的对象是否被更改了,它是,它只是*ngFor
没有更新的对象
-----------------------------更新----------------------------
我还为它指定了
changes.columns.currentValue
值,并且我在ngOnChanges
中有一个调试器,每当我更改值时,调试器都会进入该调试器。将this.columns=changes.columns.currentValue
添加到ngOnChanges
ngOnChanges(changes: SimpleChanges){
this.columns = changes.columns.currentValue;
}
您可以使用set/get方法进行更改检测 假设我们有一个名为
columnID
我们可以像下面这样编写代码
列组件:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-column',
templateUrl: './column.component.html',
styleUrls: ['./column.component.css']
})
export class ColumnComponent {
_columnID: string;
set columnID(value: string) {
this._columnID = value;
this.propChanged();
}
get columnID(): string {
return this._columnID;
}
}
你的html你可以像这样使用这个组件
<app-column [columnID]="someVariable"></app-column>
我们如何知道ngOnChanges正在执行?@dalorzo所说的是一个有效点,它实际触发了吗?如果不是,问题可能出在父级,您将值传递给输入。@Dalorzo@MikeOne它被触发了,我在该部分添加了I调试器,它输入了,我也检查了,它的值也被更改了,但是ngFor
仍然没有更新以查看相关的输入代码和模板代码。你正在做一些破坏关系的不典型的事情你在你的ngFor中使用trackBy函数吗?我已经试过了,但仍然没有更新*ngFor