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.

unexpected transition happened with router change after calling applicationRef.attachView

See original GitHub issue

🐞 bug report

Affected Package

@angular/animations @angular/router

Is this a regression?

no

Description

The unexpected transition will happen when router change after attaching the component with the applicationRef.attachView

πŸ”¬ Minimal Reproduction

Angular 8.x: https://stackblitz.com/edit/angular-router-transition-bug?file=src/app/app.component.ts Angular 9.0.0-rc.8: https://stackblitz.com/edit/github-va6d8q?file=src/app/app.component.ts

  1. click the router link, you would find the unexpected transition happened
  2. comment this line this.applicationRef.attachView(view.hostView);
  3. click the router link again, you would find the transition won’t happen anymore.

remove the BrowserAnimationsModule could also stop the transition bug, so maybe the BrowserAnimationsModule cause the root issue.

πŸ”₯ Exception or Error

the unexpected transition happened when router change

Kapture 2020-01-10 at 15 32 20

🌍 Your Environment

Angular Version:

The bug still exists in the latest angular version 9.0.0-rc.8, It breaks all transition style in our components after any overlay created with @angular/cdk


@angular-devkit/architect         0.803.21
@angular-devkit/build-angular     0.803.21
@angular-devkit/build-optimizer   0.803.21
@angular-devkit/build-webpack     0.803.21
@angular-devkit/core              8.3.21
@angular-devkit/schematics        8.3.21
@angular/cdk                      8.2.3
@angular/cli                      8.3.21
@ngtools/webpack                  8.3.21
@schematics/angular               8.3.21
@schematics/update                0.803.21
rxjs                              6.4.0
typescript                        3.5.3
webpack                           4.39.2

Anything else relevant?

I have debugged this issue with DOM breakpoint and find that the className was added in different sequence if there is an attached view, which caused this issue.

  1. the transition className was added immediately after router change if there is no attached view, this is the expected behavior. without-attach

  2. the transition className was not added after router change if there is an attached view, and the className was added in the next tick, which caused the unexpected transition. this is not the expected behavior. after-attched

Here is one ng-zorro-antd bug caused by this issue

Hope it could be fixed before angular 9 release ref @angular/cdk

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
vthinkxiecommented, Mar 12, 2020

Hi @pkozlowski-opensource Would this issue be fixed in the 9.1.0 version? Our component libs depend on the angular animation heavily, we implement a hack directive to skip this issue, but still hopes angular could fix this issue soon https://github.com/NG-ZORRO/ng-zorro-antd/blob/af4051eb11a5c9a571d7ccdea75774ea786ba990/components/core/transition-patch/transition-patch.directive.ts#L18

1reaction
atscottcommented, Oct 24, 2020

TL;DR - Jump to the bottom of this comment for possible solutions.

There’s still a fair amount I don’t know, but here’s what I understand so far:

There are a couple possible solutions for you:

  1. Preferred solution: change your host binding to be static. Static host bindings are applied immediately on creation of the component. I highly recommend this option for all static host bindings to avoid unexpected behavior like this instead of using '[class.X]': "true". So convert host: {"[class.transition]": "true"} to host: {"class": "transition"}.
  2. If solution #1 is not an option for you because your host binding really is static, maybe you can call applicationRef.attachView in the ngOnInit of ApplicationRef. This will mean that the root view with the router outlet is processed first
  3. (a tiny bit messy because it involves casting your forked class to a RouterOutlet to pass to parentContexts.onChildOutletCreated): Fork router-outlet and in activateWith, call this.changeDetector.detectChanges(); instead of or right before this.changeDetector.markForCheck();. This will cause the change detection to run for your created component immediately and the host binding should be applied before the browser can β€œsee” that the class wasn’t there at the start.

Given that there are several possible solutions here, I’m setting the priority lower. I don’t think there’s much that can be done in the core package to fix this. Dynamic bindings have to be processed later because they could depend on component data that in turn depends on change detection running. This could be addressed in the router-outlet by calling detectChanges in activateWith like option #3, but I’m wary of making this change because it might cause other unexpected failures (changing the behavior of CD in the core components is quite often a breaking change).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Igor Minar on Twitter: "Btw unless new issues are identified ...
unexpected transition happened with router change after calling applicationRef.attachView Β· Issue... bug report Affected Package @angular/animationsΒ ...
Read more >
ApplicationRef - Angular
In this case, Angular throws an error, since an Angular application can only have one change detection pass during which all change detection...
Read more >
What is the purpose of ApplicationRef in Angular 2+?
allows to invoke application-wide change detection by calling appRef. Β· it allows to add/remove views to be included in or excluded from change...
Read more >
ε·ε€–οΌšAngular 4 ζ­£εΌη‰ˆε‘εΈƒοΌοΌ - ζŽ˜ι‡‘
fix(router): do not finish bootstrap until all the routes are ... router: call resolver when upstream params change (#12942) (d4d3782)Β ...
Read more >
Clipcode Source Tour | PDF | Application Programming Interface ...
Application code can make zone calls if needed and ... Zones are how Angular initiates change detection – when the zone's mini-stack is...
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