question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ivy] using a template reference before its declaration causes ExpressionChangedAfterItHasBeenCheckedError

See original GitHub issue

🐞 bug report

Affected Package

Angular 9-rc.9 with Ivy

Is this a regression?

This issue is present only when Ivy is enabled. I have to say that I am not 100% sure it’s a bug. It could be the pattern we have been using might not have been supported by Angular in the first place. But I have my own doubts as I explained below.

Description

Below is a simplified reproduction of the pattern that’s causing the ExpressionChangedAfterItHasBeenCheckedError error. The following snippet would work just in any Angular version as long as Ivy is not enabled:

<h1>Greetings, {{hello.name}}!</h1>
<hello-comp #hello [name]="'John'"></hello-comp>

As you can see, I’m simply template-referencing a component and interpolating its name property right above it.

One thing to note here is that if you move the interpolation down below the hello-comp, I see there is no error:

<hello-comp #hello [name]="'John'"></hello-comp>
<h1>Greetings, {{hello.name}}!</h1>

So basically, Ivy is not happy when the template reference is used before its declaration. However, we had been using this pattern without any issues before Ivy. Also, I suspected that this pattern (using a template reference before its declaration) could have been something Angular has never supported. But in the documentation, I read these:

  • The scope of a reference variable is the entire template.
  • You can refer to a template reference variable anywhere in the component’s template

So it seems to me at least using a template reference before its declaration has always been supported/allowed. But when Ivy is enabled, it starts throwing the error. As such, I am suspecting this could be another ivy related bug.

πŸ”¬ Minimal Reproduction

https://stackblitz.com/edit/angular-8-template-reference-interpolation

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
acohenOTcommented, Feb 27, 2020

This error happens when using route animations using the sample code provided https://angular.io/guide/route-animations#router-outlet

outlet is used before it’s declared

<div [@routeAnimations]="prepareRoute(outlet)" >
  <router-outlet #outlet="outlet"></router-outlet>
</div>
0reactions
angular-automatic-lock-bot[bot]commented, Nov 16, 2020

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Everything you need to know about the ... - InDepth.Dev
In its template it uses the expression that references name property: ... has and so Angular throws the error ExpressionChangedAfterItHasBeenCheckedError .
Read more >
angular - How can I fix this specific NG0100 - Stack Overflow
It's because you are executing some code in the ngAfterViewInit , which modifies the data that is displayed. First detection cycle evaluatedΒ ...
Read more >
Angular Debugging "Expression has changed": Explanation ...
Learn a complete explanation about ExpressionChangedAfterItHasBeenCheckedError: why it occurs, how to troubleshoot it and how to fix it.
Read more >
Common Angular Errors And How To Fix Them - Briebug Blog
A change that occurred with Ivy requires you declare an object ... This error occurs when you reference a directive in a template...
Read more >
Ivy compatibility examples - Angular
In this case, it means that the value binding will be checked before the *ngFor is checked, as it is declared above the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found