ExpressionChangedAfterItHasBeenCheckedError Explained

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

    Here's my thoughts on what is happening. I have not read the documentation but am sure this is part of why the error is shown.

    *ngIf="isProcessing()" 
    

    When using *ngIf, it physically changes the DOM by adding or removing the element every time the condition changes. So if the condition changes before it is rendered to the view (which is highly possible in Angular's world), the error is thrown. See explanation here between development and production modes.

    [hidden]="isProcessing()"
    

    When using [hidden] it does not physically change the DOM but merely hiding the element from the view, most likely using CSS in the back. The element is still there in the DOM but not visible depending on the condition's value. That is why the error will not occur when using [hidden].

提交回复
热议问题