演示: https://stackblitz.com/angular/mxoemeygmlk?file=app%2Ftable-http-example.ts
解决这个 Angular 误差的最佳解决方案是什么:
最佳答案
我知道的解决方案(黑客)是使用 setTimeout(function , 0)
它将您的函数发送到事件循环的末尾,直到 Angular 完成检查,如下所示:
constructor(private http: HttpClient) {}
ngAfterViewInit() {
setTimeout(() => this.ngAfterViewInitUtil(), 0);
}
ngAfterViewInitUtil(){
this.exampleDatabase = new ExampleHttpDao(this.http);
// The rest of the code
另请注意,此错误仅在开发模式下发生,而在生产模式下不会发生,因为 Angular 不会在生产模式下重新检查应用程序的状态。
您可以在this page中阅读有关此错误的更多信息。
关于javascript - ExpressionChangedAfterItHasBeenCheckedError : Expression has changed after it was checked. 先前值 : 'ngIf: false' . 当前值: 'ngIf: true',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433819/