javascript - ExpressionChangedAfterItHasBeenCheckedError : Expression has changed after it was checked. 先前值 : 'ngIf: false' . 当前值: 'ngIf: true'

标签 javascript html angular

演示: https://stackblitz.com/angular/mxoemeygmlk?file=app%2Ftable-http-example.ts

解决这个 Angular 误差的最佳解决方案是什么:

enter image description here

最佳答案

我知道的解决方案(黑客)是使用 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/

相关文章:

angular - 从 *ngFor 中的选择元素获取值

javascript - .joinedAt 属性总是在discord.js 中返回未定义

javascript - 如何使用 PJAX 设置自定义 xhr header ?

javascript - 更改xtype菜单extjs的字体颜色和样式

javascript - 使用javascript上下滚动

javascript - 在 CSS 中,我不知道如何将 "content"添加到 "first-line"或 "first-letter"

html - 图的非固定高度(amCharts)

Angular 从版本 5 降级到版本 4

javascript - 每 x 毫秒获取 jquery 触摸位置

javascript - Angular 4.我无法使用自动完成 - primeNG