angular: ExpressionChangedAfterItHasBeenCheckedError when changing a component 'non model' value in afterViewInit

I’m submitting a …

[x] bug report
[ ] feature request
[ ] support request

Current behavior changing a component ‘non model’ value in afterViewInit throws ExpressionChangedAfterItHasBeenCheckedError if the value has an impact on a method used in the view.

in the plunker: -> getTextDecoration() returns none before ngAfterViewInit is called (isViewInit === false) -> getTextDecoration() returns underline after ngAfterViewInit is called (isViewInit === true)

Expected behavior it should not throw the error since getTextDecoration is just a method and not part of the model.

Minimal reproduction of the problem with instructions http://plnkr.co/edit/k8AsppUH8V2RvPeLJ0JT?p=preview

  • Angular version: 4.0.0 => KO
  • Angular version: 4.0.0-beta.8 => OK

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 10
  • Comments: 15 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I had a similar issue when I wanted to dynamically create a component with resolveComponentFactory and inserting the created component back into the template. Looking at the lifecyle hooks documentation, I changed ngAfterViewInit to ngAfterContentInit and it worked.

Hope it helps 😉

Check the Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError error article for the in-depth explanation of the cause of the error and the need for the check that produces the error.

this happend to me if i use time ago pipe xD CboxComponent.html:37 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '49 minutes ago'. Current value: '50 minutes ago'.

This is works as intended, as you are using getTextDecoration() in your template. Therefore Angular will call it whenever it dirty checks your template. I.e. Angular only looks at the resulting values of expressions…

In dev mode (i.e. you didn’t call enableProdMode()), angular does 2 dirty checks:

  • 1x to apply new values and
  • 1x afterwards to check that nothing has changed (to enforce the unidirectional data flow).