ExpressionChangedAfterItHasBeenCheckedError:使用Angular2检查表达式后发生了变化

时间:2018-01-08 04:20:06

标签: javascript angular typescript

我试图以这种方式解决此错误(venv_OpenRefine) localhost@xcEngine $ python xcEngine.py Traceback (most recent call last): File "xcEngine.py", line 54, in <module> something() File "xcEngine.py", line 51, in something XCEngine.ruleswriter(some) TypeError: unbound method ruleswriter() must be called with XCEngine instance as first argument (got list instance instead)

TS:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'.

当我使用 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); }); } } 时,错误在控制台中得到了解决,但我还面临一个问题,除非我在页面打开时点击,否则页面不会显示任何数据。一旦我随机点击页面,数据就会加载。任何人都可以帮助我。

2 个答案:

答案 0 :(得分:0)

首先删除changeDetection: ChangeDetectionStrategy.OnPush

其次,你有:
public Pages:Object; =&gt;避免对变量使用第一个大写字母,因此请改用public pages:Object;

然后你在这里:

 Pages => {
    this.Pages = Pages.data;
}

尝试:

response => {
    this.pages = response.data;
}

答案 1 :(得分:0)

我认为你需要提供初始值。

例如 -

public Pages = null;

有关更多信息,请阅读本文 - Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError error。它非常详细地解释了这个错误。希望它有所帮助。