ExpressionChangedAfterItHasBeenCheckedError Explained

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

    This error indicates a real problem in your application, therefore it makes sense to throw an exception.

    In devMode change detection adds an additional turn after every regular change detection run to check if the model has changed.

    If the model has changed between the regular and the additional change detection turn, this indicates that either

    • change detection itself has caused a change
    • a method or getter returns a different value every time it is called

    which are both bad, because it is not clear how to proceed because the model might never stabilize.

    If Angular runs change detection until the model stabilizes, it might run forever. If Angular doesn't run change detection, then the view might not reflect the current state of the model.

    See also What is difference between production and development mode in Angular2?

提交回复
热议问题