Angular 为什么模型没有更新?
我有一个自定义函数:Angular 为什么模型没有更新?,angular,angular5,Angular,Angular5,我有一个自定义函数: public changeClass() { this.studyPlan = this.studyPlanClone.slice(); if (this.filter.classNumber) { this.studyPlan = this.studyPlan.filter(study => study.classNumber == this.filter.classNumber); } console.log(this
public changeClass() {
this.studyPlan = this.studyPlanClone.slice();
if (this.filter.classNumber) {
this.studyPlan = this.studyPlan.filter(study => study.classNumber == this.filter.classNumber);
}
console.log(this.studyPlan);
}
HTML模板为:
<tr *ngFor="let item of studyPlan"></tr>
当我调用changeClass()
方法时,它过滤存在的初始对象并返回一个新对象
我可以在控制台中看到3个对象的正确结果:
但是在页面上,我看到了prev object
studyPlan
。它不会被渲染。我们可能需要手动触发更改检测。尝试在setTimeout()内添加changeClass()块
我没有太多的代码要处理,但我要检查的第一件事是,是否将变更检测设置为OnPush。这是“我在.ts中更新了变量,但在浏览器中没有更改”的最常见原因 在这里读一下 其次,我不知道Suresh是什么意思,通过超时和随机函数调用手动触发。如果要手动触发更改检测,请使用适当的类/函数 在构造函数中导入
private _cdRef: ChangeDetectorRef
当您想要检测更改时,请运行
this._cdRef.detectChanges();
{{item.id}
考虑留下一些代码或a来显示您的问题和整个代码。原因是什么?
this._cdRef.detectChanges();