Visitar URL original
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for '@routeAnimations': 'undefined'. Current value: 'NewAnimationName'. · Issue #36173 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20230329041822/https://github.com/angular/angular/issues/36173
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for '@routeAnimations': 'undefined'. Current value: 'NewAnimationName'. #36173

Closed
acohenOT opened this issue Mar 20, 2020 · 17 comments
Assignees
Labels
area: animations area: docs Related to the documentation area: router freq2: medium P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent state: confirmed type: bug/fix
Milestone

Comments

@acohenOT
Copy link
Contributor

acohenOT commented Mar 20, 2020 •

RouteAnimationError.zip

🐞 bug report

Affected Package

Not clear.

Possibly @angular/animations or @angular/router

Is this a regression?

Yes, the previous version in which this bug was not present was: 8.3

Description

Using route animation with a parent element that is conditionally hidden, causes ExpressionChangedAfterItHasBeenCheckedError. This did not happen in Angular 8.3. Began with Angular 9.

Possibly related to issue #34821.

Error occurs with

<div *ngIf="!mobileQuery.matches">
    <div [@routeAnimations]="prepareRoute(outlet)">
        <router-outlet #outlet="outlet"></router-outlet>
    </div>
</div>

No error occurs with

<div>
    <div [@routeAnimations]="prepareRoute(outlet)">
        <router-outlet #outlet="outlet"></router-outlet>
    </div>
</div>

🔬 Minimal Reproduction

Unable to recreate in stackblitz. See attached min repro project.

Steps:

  1. Unzip attached project
  2. Run npm install
  3. Run ng serve --open=true
  4. Navigate to /home URL
  5. Open Developer tools, notice error

screenshot

@atscott
Copy link
Contributor

atscott commented Mar 24, 2020

#36222 also had a minimal repro that's a bit smaller: https://github.com/fxck/angular-animation-cd-bug

@pkozlowski-opensource pkozlowski-opensource added the regression Indicates than the issue relates to something that worked in a previous version label Mar 24, 2020
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Apr 7, 2020
@marcoripa96
Copy link

I have the same issue in angular 9.1.

@bhanna1693
Copy link

I have the same issue. I think this is an important issue. The [hidden] attribute is a good substitute (sometimes) but if I need to wait for information before rendering the child routes, the ngif is really necessary. A resolver might work as well, but that simply doesn't work for my use case all the time.

@acohenOT
Copy link
Contributor Author

acohenOT commented Aug 8, 2020

This bug exists in Angular 10, as well.

@digi-ratio
Copy link

core.js:6185 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.
at throwErrorIfNoChangesMode (core.js:8092)
at bindingUpdated (core.js:19773)
at Module.ɵɵproperty (core.js:20921)
at LayoutAfterLoginComponent_Template (layout-after-login.component.html:34)
at executeTemplate (core.js:11949)
at refreshView (core.js:11796)
at refreshComponent (core.js:13229)
at refreshChildComponents (core.js:11527)
at refreshView (core.js:11848)
at refreshDynamicEmbeddedViews (core.js:13154)

There is any solution for this bug?

@mikula96
Copy link

mikula96 commented Aug 21, 2020 •

Did anyone find something on this?
I have same problem when using generated angular material menu

core.js:6272 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'true'. Current value: 'false'.
at throwErrorIfNoChangesMode (core.js:8191)
at bindingUpdated (core.js:20160)
at ɵɵproperty (core.js:21311)
at NavigationBarComponent_Template (navigation-bar.component.html:3)
at executeTemplate (core.js:12141)
at refreshView (core.js:11980)
at refreshComponent (core.js:13461)
at refreshChildComponents (core.js:11701)
at refreshView (core.js:12036)
at refreshComponent (core.js:13461)

The link should call backend API for showing some data but I get this error instead.

@hhsissi
Copy link

hhsissi commented Aug 27, 2020 •

Hi, did anyone find how to fix this ? Same problem as @acohenOT, error pops up when @routerAnimations directive is in a *ngIf.

Edit

Ok i found a fix that worked for me, kinda dirty but it works:

1- Inject changeRef service

constructor(private changeRef: ChangeDetectorRef) { }
Implement AfterViewChecked interface

ngAfterViewChecked(): void { this.changeRef.detectChanges(); }

@jelbourn jelbourn added the P2 The issue is important to a large percentage of users, with a workaround label Oct 1, 2020
@dmoore1181
Copy link

dmoore1181 commented Oct 19, 2020 •

I am also getting this issue when attempting to use activated route data to trigger route animation. The animation works, just want to get rid of the error message to keep from hampering future debugging when we haven't been working in this project for a while.

<div style="overflow: hidden" [@projectAnimation]="projectOutlet && projectOutlet.activatedRouteData && projectOutlet.activatedRouteData['animation']">
      <router-outlet style="overflow: hidden" #projectOutlet="outlet"></router-outlet>
</div>

This is using Angular 9.2.4

@israeldesouza
Copy link

israeldesouza commented Feb 10, 2021 •

I found this https://angular.io/errors/NG0100

@irowbin
Copy link

irowbin commented Apr 26, 2021

+1 Getting same error.

@Davste93
Copy link

Same issue Angular 12.0.3

@aminetchitooss
Copy link

This Fixed the problem for me :

<main [@routeAnimations]="prepareRoute(outlet)">
    <router-outlet class="route" #outlet="outlet"></router-outlet>
</main>
import { RouterOutlet } from '@angular/router';
import { AfterViewInit, ChangeDetectorRef } from '@angular/core';

export class HomeComponent implements AfterViewInit {


  constructor(private changeRef: ChangeDetectorRef) { }

  ngAfterViewInit(): void {
     this.changeRef.detectChanges();
  }

  prepareRoute(outlet: RouterOutlet) {
      return outlet?.activatedRouteData?.animation;
  }
 
}

@dario-piotrowicz
Copy link
Contributor

dario-piotrowicz commented Jan 8, 2022 •

For what it's worth I think that this is not at all animations related

I think the issue is related to the router-outlet's data value and that just happens to be often used for routing animations but it is not specific to routing animations in the least, it can really just be used for anything

I've created a minimal reproduction which doesn't use animations anywhere (it does not even import the animations module): https://github.com/dario-piotrowicz/angular-expressionchanged-router-bug (the important bit is just in the AppComponent)

PS: this still happens in Angular 13 as you can see from the repo

@alxhub alxhub added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent and removed P2 The issue is important to a large percentage of users, with a workaround labels Feb 11, 2022
@atscott
Copy link
Contributor

atscott commented Feb 14, 2022

This happens because the template attempts to read from the activated route data, which isn't updated until ngOnInit.

Angular does not support reading values from a component which change during change detection before the component executes.

The reason this works when the outlet element is always present (rather than conditionally) is because the Router is able to locate the RouterOutlet during navigation, activate it, and then trigger a change detection run so everything is updated correctly.

We could consider activating the outlet in the constructor or at least setting the _activatedRoute there since we know the answer to that immediately. I'm hesitant to do this though since it's really just a workaround for a data access pattern that shouldn't exist.

Instead, the way this should be done is through the public context data. For Dario's example, this would be

  constructor(private contexts: ChildrenOutletContexts) {
  }

  getRouteDataText() {
    return this.contexts.getContext('primary')?.route?.snapshot?.data?.['text'];
  }

@dario-piotrowicz Would you like to take a look at the documentation and update it to not show this backwards reference data access?

@atscott atscott added area: docs Related to the documentation and removed regression Indicates than the issue relates to something that worked in a previous version engine: ivy labels Feb 14, 2022
@dario-piotrowicz
Copy link
Contributor

dario-piotrowicz commented Feb 14, 2022 •

This happens because the template attempts to read from the activated route data, which isn't updated until ngOnInit.

Angular does not support reading values from a component which change during change detection before the component executes.

The reason this works when the outlet element is always present (rather than conditionally) is because the Router is able to locate the RouterOutlet during navigation, activate it, and then trigger a change detection run so everything is updated correctly.

We could consider activating the outlet in the constructor or at least setting the _activatedRoute there since we know the answer to that immediately. I'm hesitant to do this though since it's really just a workaround for a data access pattern that shouldn't exist.

Instead, the way this should be done is through the public context data. For Dario's example, this would be

  constructor(private contexts: ChildrenOutletContexts) {
  }

  getRouteDataText() {
    return this.contexts.getContext('primary')?.route?.snapshot?.data?.['text'];
  }

@dario-piotrowicz Would you like to take a look at the documentation and update it to not show this backwards reference data access?

@atscott sure! I'd love to! 😁

Feel free to assign the issue to me if you want 😃 (ah no, you've got it assigned! ok sure we can keep it like that 😄)

@atscott atscott assigned dario-piotrowicz and unassigned atscott Feb 14, 2022
@dario-piotrowicz
Copy link
Contributor

Thanks @atscott 😄

dario-piotrowicz added a commit to dario-piotrowicz/angular that referenced this issue Feb 17, 2022
developers should not access the router-outlet directive in their
template before defining a template variable for it, such
implementation is present in a couple of aio guides, fix such guides
so that they show the more correct way of accessing the outlet's data

resolves angular#36173
dario-piotrowicz added a commit to dario-piotrowicz/angular that referenced this issue Feb 18, 2022
developers should not access the router-outlet directive in their
template before defining a template variable for it, such
implementation is present in a couple of aio guides, fix such guides
so that they show the more correct way of accessing the outlet's data

resolves angular#36173
jessicajaniuk pushed a commit that referenced this issue Feb 24, 2022
developers should not access the router-outlet directive in their
template before defining a template variable for it, such
implementation is present in a couple of aio guides, fix such guides
so that they show the more correct way of accessing the outlet's data

resolves #36173

PR Close #45140
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Mar 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: animations area: docs Related to the documentation area: router freq2: medium P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent state: confirmed type: bug/fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.