[英]ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked using Angular2
i had tried to solve this error 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'.
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'.
in this way: 通过这种方式:
TS: 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);
});
}
}
When i used ChangeDetectionStrategy
the error got solved in console but i am facing one more issue, the page is not showing any data unless i give a click when the page opens. 当我使用
ChangeDetectionStrategy
该错误已在控制台中解决,但我面临另一个问题,除非页面打开时我单击一下,否则该页面未显示任何数据。 Once i give a random click on page the data loads. 一旦我在页面上随机单击,数据就会加载。 Can anyone help me.
谁能帮我。
First Delete changeDetection: ChangeDetectionStrategy.OnPush
首先删除
changeDetection: ChangeDetectionStrategy.OnPush
Secondly, you have: 其次,您有:
public Pages:Object;
=> avoid using a First capital letter for variables, so use public pages:Object;
=>避免对变量使用首字母大写,因此请使用
public pages:Object;
instead. 代替。
then here you have: 那么你在这里:
Pages => {
this.Pages = Pages.data;
}
try: 尝试:
response => {
this.pages = response.data;
}
I think you need to provide initial value. 我认为您需要提供初始价值。
for example - 例如 -
public Pages = null;
for more information read this article - Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError
error . 有关更多信息,请阅读本文- 您需要了解有关
ExpressionChangedAfterItHasBeenCheckedError
错误的所有信息 。 It explain this error in great detail. 它详细解释了此错误。 Hope it helps.
希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.