![](/img/trans.png)
[英]change dynamic color - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
[英]ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked using Angular2
我曾尝试解决此错误ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'.
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'.
通过这种方式:
TS:
import {
ChangeDetectionStrategy
} from '@angular/core';
@Component({
templateUrl: './page.html'
changeDetection: ChangeDetectionStrategy.OnPush
})
public Pages: Object;
ngOnInit() {
loadAllGuides() {
this.ApiService
.getAllPages()
.subscribe(
Pages => {
this.Pages = Pages.data;
}, error => {
console.log(error);
});
}
}
当我使用ChangeDetectionStrategy
该错误已在控制台中解决,但我面临另一个问题,除非页面打开时我单击一下,否则该页面未显示任何数据。 一旦我在页面上随机单击,数据就会加载。 谁能帮我。
首先删除changeDetection: ChangeDetectionStrategy.OnPush
其次,您有:
public Pages:Object;
=>避免对变量使用首字母大写,因此请使用public pages:Object;
代替。
那么你在这里:
Pages => {
this.Pages = Pages.data;
}
尝试:
response => {
this.pages = response.data;
}
我认为您需要提供初始价值。
例如 -
public Pages = null;
有关更多信息,请阅读本文- 您需要了解有关ExpressionChangedAfterItHasBeenCheckedError
错误的所有信息 。 它详细解释了此错误。 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.