0

hi I get this error "expression has been changed after it was checked" in angular but behavior of my app is true. first question is if error happen why component work correctly?

for example I have 2 brother component that use them like this:

<cmp-a (onSelectItem)="selectedItem = $event"></cmp-a>
<cmp-b [selectedItem]="selectedItem"></cmp-b>

when onSelectItem in cmp-a emitted i get error.but selectedItem correctly pass to cmp-b and all things is very good and ok!!!

and second question is if really an error happen why in prod mode not display this error by angular framework?

thanks

2
  • 1
    This is a non-blocking error, made for you to notice that you are messing with the component lifecycle. Since it is non-blocking, it won't appear in the final build. Commented Jan 15, 2019 at 13:47
  • It is working because this error doesn't cause a crash. It just means something happened which angular doesn't expect regarding the lifecycle of the component. Commented Jan 15, 2019 at 13:51

1 Answer 1

2

I hope this will help you out. I don't like using links cause it may broke in the future. So here is what you need to know about this type of error.

A running Angular application is a tree of components. During change detection Angular performs checks for each component which consists of the following operations performed in the specified order:

  • update bound properties for all child components/directives
  • call ngOnInit, OnChanges and ngDoCheck lifecycle hooks on all child components/directives
  • update DOM for the current component
  • run change detection for a child component
  • call ngAfterViewInit lifecycle hook for all child components/directives

There are other operations that are performed during change detection.

After each operation Angular remembers what values it used to perform an operation. They are stored in the oldValues property of the component view. After the checks have been done for all components Angular then starts the next digest cycle but instead of performing the operations listed above it compares the current values with the ones it remembers from the previous digest cycle:

  • check that values passed down to the child components are the same as the values that would be used to update properties of these components now
  • check that values used to update the DOM elements are the same as the values that would be used to update these elements now
  • perform the same checks for all child components

Please note that this additional check is only performed in the development mode.

2nd question: It seems that recently almost every day there’s a question on stackoverflow regarding the ExpressionChangedAfterItHasBeenCheckedError error thrown by Angular. Usually these questions come up because Angular developers do not understand how change detection works and why the check that produces this error is required. Many developers even view it as a bug. But it’s certainly not.

U can find a good example here at the title: Causes of values change. https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4

Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much my friend. angular detect an error in change detection and this means this binding should not work but my property correctly pass to another component?!!!
Still having the issue? If the answer was satisfying please give it an accept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.