ExpressionChangedAfterItHasBeenCheckedError Explained

前端 未结 26 1933
慢半拍i
慢半拍i 2020-11-22 14:46

Please explain to me why I keep getting this error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.

Obviously,

26条回答
  •  一向
    一向 (楼主)
    2020-11-22 15:45

    Angular runs change detection and when it finds that some values which has been passed to the child component have been changed, Angular throws the following error:

    ExpressionChangedAfterItHasBeenCheckedError click for more

    In order to correct this we can use the AfterContentChecked life cycle hook and

    import { ChangeDetectorRef, AfterContentChecked} from '@angular/core';
    
      constructor(
      private cdref: ChangeDetectorRef) { }
    
      ngAfterContentChecked() {
    
        this.cdref.detectChanges();
    
      }
    

提交回复
热议问题