要解决Angular 4.4中页面不会根据模型的更改更新的问题,需要确保正确使用了Angular的数据绑定机制。以下是一个包含代码示例的解决方法:
export class MyComponent {
myModel: string;
constructor() {
this.myModel = '初始值';
}
}
import { ChangeDetectorRef } from '@angular/core';
export class MyComponent {
myModel: string;
constructor(private cdr: ChangeDetectorRef) {
this.myModel = '初始值';
}
updateModel(newValue: string) {
this.myModel = newValue;
this.cdr.detectChanges(); // 手动通知Angular进行变更检测
}
}
通过调用ChangeDetectorRef
的detectChanges()
方法,可以手动通知Angular进行变更检测,以确保页面能够根据模型的更改进行更新。