简体   繁体   English

角度变化检测错误:ExpressionChangedAfterItHasBeenCheckedError

[英]Angular change detection error: ExpressionChangedAfterItHasBeenCheckedError

I am getting this error when input binding value is changed inside the component from null/undefiend to some value. 当组件内部的输入绑定值从null / undefiend更改为某个值时,出现此错误。 Below is the URL for sample code. 以下是示例代码的URL。

https://stackblitz.com/edit/angular-h61csi https://stackblitz.com/edit/angular-h61csi

Why do I get this error? 为什么会出现此错误? Someone please explain me how to resolve this without setTimeout. 有人请向我解释如何在没有setTimeout的情况下解决此问题。

You can put setting this.selectedItem in a setTimeout . 您可以将设置this.selectedItem放在setTimeout

setTimeout(() => {
  if(!this.selectedItem) {
    this.selectedItem = this.items[1];
    this.cdr.markForCheck();
  }
});

To know why and when this error occurs you can read about ExpressionChangedAfterItHasBeenCheckedError here - https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4 要知道为什么以及何时发生此错误,您可以在此处阅读有关ExpressionChangedAfterItHasBeenCheckedError -https: //blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasenenedederror-error-error-e3fd9ce7dbb4

Totally agree with @vatz response with small correction. 完全同意@vatz响应,并进行了较小的校正。 We don't need 'this.cdr.markForCheck()', its required only when we have onpush change detection used in the component. 我们不需要'this.cdr.markForCheck()',仅当我们在组件中使用了onpush更改检测时才需要它。

setTimeout(() => {
  if(!this.selectedItem) {
    this.selectedItem = this.items[1];
  }
});

I think the best way to resolve your issue is to redesign your components a bit. 我认为解决问题的最佳方法是重新设计组件。 Your child component shouldn't pass values back to the parent component, especially if the parent component already knows what the values are, ie the AppComponent in your case, knows exactly what the values of the selectedItem and the items array are, so you can put the logic from the AfterViewInit hook of the child component in the parent component. 您的子组件不应将值传递回父组件,尤其是如果父组件已经知道值是什么,即您的AppComponent确切知道selectedItem和items数组的值是什么,那么您可以将子组件的AfterViewInit挂钩中的逻辑放在父组件中。 Simply change the selectedItem field to: 只需将selectedItem字段更改为:

private _selectedItem: Object;

public get selectedItem() {
    return this._selectedItem ? this._selectedItem : this.items[1];
}

And then remove the code from the AfterViewInit hook in the child component and everything should work without errors. 然后从子组件中的AfterViewInit挂钩中删除代码,一切都应该正常工作。 Otherwise, you will have to use setTimeout. 否则,您将不得不使用setTimeout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular4更改检测ExpressionChangedAfterItHasBeenCheckedError - Angular4 Change Detection ExpressionChangedAfterItHasBeenCheckedError 使用ngOnChanges投掷错误“ ExpressionChangedAfterItHasBeenCheckedError”进行角度材料更改检测 - Angular Material Change Detection using ngOnChanges throwing error, “ExpressionChangedAfterItHasBeenCheckedError” 角度错误:ExpressionChangedAfterItHasBeenCheckedError - Angular Error: ExpressionChangedAfterItHasBeenCheckedError Angular 加载错误 ExpressionChangedAfterItHasBeenCheckedError - Angular Loading Error ExpressionChangedAfterItHasBeenCheckedError 错误:ExpressionChangedAfterItHasBeenCheckedError 变量 Angular? - Error: ExpressionChangedAfterItHasBeenCheckedError varaible Angular? Angular 4应用程序中的ExpressionChangedAfterItHasBeenCheckedError错误 - ExpressionChangedAfterItHasBeenCheckedError Error in Angular 4 Application ExpressionChangedAfterItHasBeenCheckedError`错误角度4 - ExpressionChangedAfterItHasBeenCheckedError` error angular 4 ExpressionChangedAfterItHasBeenCheckedError和上下文错误Angular 4 - ExpressionChangedAfterItHasBeenCheckedError and context Error Angular 4 错误:ExpressionChangedAfterItHasBeenCheckedError-角度 - Error: ExpressionChangedAfterItHasBeenCheckedError - Angular 角度和错误:ExpressionChangedAfterItHasBeenCheckedError - Angular and ERROR: ExpressionChangedAfterItHasBeenCheckedError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM