ExpressionChangedAfterItHasBeenCheckedError Explained

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

    My issue was manifest when I added *ngIf but that wasn't the cause. The error was caused by changing the model in {{}} tags then trying to display the changed model in the *ngIf statement later on. Here's an example:

    {{changeMyModelValue()}}
    ....
    {{myModel.value}}

    To fix the issue, I changed where I called changeMyModelValue() to a place that made more sense.

    In my situation I wanted changeMyModelValue() called whenever a child component changed the data. This required I create and emit an event in the child component so the parent could handle it (by calling changeMyModelValue(). see https://angular.io/guide/component-interaction#parent-listens-for-child-event

提交回复
热议问题