ExpressionChangedAfterItHasBeenCheckedError Explained

前端 未结 26 2000
慢半拍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:35

    Follow the below steps:

    1. Use 'ChangeDetectorRef' by importing it from @angular/core as follows:

    import{ ChangeDetectorRef } from '@angular/core';
    

    2. Implement it in constructor() as follows:

    constructor(   private cdRef : ChangeDetectorRef  ) {}
    

    3. Add the following method to your function which you are calling on an event like click of button. So it look like this:

    functionName() {   
        yourCode;  
        //add this line to get rid of the error  
        this.cdRef.detectChanges();     
    }
    

提交回复
热议问题