angular: Leave animation triggers child ngOnDestroy immediately
I’m submitting a…
[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
ngOnDestroy is called when the :leave animation starts.
Expected behavior
ngOnDestroy should be called after the :leave animation finishes.
Minimal reproduction of the problem with instructions
- Wait for the
:enteranimation to finish. The element background is red during the whole duration of the animation. - Click the “Hide” button. The red background is removed immediately when the animation starts.
If replays are necessary, use the “Show” button to restart the reproduction.
What is the motivation / use case for changing the behavior?
If the destroy method makes visual changes to the component or directive, these will be visible during the leave animation. This results in unexpected behavior for the user.
Environment
Angular version: 4.4.6
Also reproducible in 5.0.0-rc.3
Browser:
- all
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 37
- Comments: 19 (2 by maintainers)
I just wanted to reiterate the issue @zip-fa brought up. I am having the same issue in Angular 18.0.1.
To introduce the error, all I had to do was change the following in a “OffcanvasComponent” that emits its state as its animation changes.
To
Sorry for spamming the whole team’s mail boxes, but can confirm - same issue here if emitting to output or setting model signal’s value
I am having this issue as well within Angular 9.1.7 and DevExtreme 20.1.7. Is there any timeline on an expected fix?
In v17.3.0, emitting with an output signals in a
(@animation.done)will throw an error :IMO, component destruction should happen after the end of the leave animation.
As temporal workaround, I wrap ouput emits with a
if (event.totalTime > 0)but I’m not sure this is really safeWe’ve reached Angular 12 and the issue still seems to persist. Looks like this won’t be changed at all I guess.
Now is angular 8 and I still have this problem
If you are calling a reactive process (like HttpClient rest call) this process will take some time or if you are using setTimeout (microtask). If the component is destroyed before it is finished, this process will continue … If you try to access any object or reference in the destroyed component after the process is subscribed, you will have problems. In fact, even if this subscription is HttpClient, it will not be unsubscribed. This actually show us HttpClient subscriptions should be destroyed manually even if they are destroyed automatically by Angular.
I also need to do something when the component is ACTUALLY destroyed, AFTER running the animation. The hook is called “onDestroy” but actually the component is still existent when an animation is running.
Is there any workaround?
Too bad this is open since 4 years now 😦
Unfortunately we can’t change this around for the current Angular. Perhaps including a new lifecycle hook in the future to handle this would be a solution.