Skip to content

Leave animation triggers child ngOnDestroy immediately #19826

Closed as not planned
Closed as not planned

Description

@gyoshev

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

Reproduction plunkr

  1. Wait for the :enter animation to finish. The element background is red during the whole duration of the animation.
  2. 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

Activity

  1. added
    area: animations
    on Nov 11, 2017
  2. Florent-Passerelle commented on Nov 20, 2017

    @Florent-Passerelle

    Same here and really annoying when using third party modules...

    Otherwise we can listen to .done event :

    @HostListener('@exitTransition.done', ['$event'])
    animationEnded(e:AnimationTransitionEvent) {
        if (e.toState == 'void') {
          console.log("Actual destroy");
       }
    }
  3. added this to the needsTriage milestone on Feb 26, 2018
  4. matsko commented on May 1, 2018

    @matsko
    Contributor

    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.

  5. modified the milestones: needsTriage, Backlog on May 1, 2018
  6. lorado commented on Aug 1, 2018

    @lorado

    Hello. Can you say, when do you plan to implement a solution?

    The described behaviour by @gyoshev exists also in angular 6.1. I created an example on stackblitz: https://stackblitz.com/edit/angular-s1ikhg

  7. ds-codemag commented on Jul 17, 2019

    @ds-codemag

    Now is angular 8 and I still have this problem

  8. Ploppy3 commented on Feb 14, 2020

    @Ploppy3

    Is it possible to fix this with Angular 9 Ivy ?

  9. waterplea commented on May 4, 2020

    @waterplea
    Contributor

    Still an issue. My components implement a breakdown logic which I wanted to delay a bit when they are removed from ngFor, animations would work just fine for it but unfortunately ngOnDestroy is called immediately :(

  10. charlie-lk commented on Jul 14, 2020

    @charlie-lk

    This issue renders the routing animations utterly useless.

  11. carlescs commented on Sep 2, 2020

    @carlescs

    Is there any timeline for fixing this? We have a lot of problems with animations with DevExtreme components inside.

    Angular version: 9.1.0 (Ivy)
    DevExtreme: 20.1.6

  12. 2 remaining items

  13. dmoore1181 commented on Oct 8, 2020

    @dmoore1181

    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?

  14. dodlhuat commented on Aug 13, 2021

    @dodlhuat

    We've reached Angular 12 and the issue still seems to persist. Looks like this won't be changed at all I guess.

  15. EchopixUser commented on Nov 8, 2021

    @EchopixUser

    Will this ever be fixed?? An issue of this magnitude from 2017 should've already been fixed IMO.

  16. MickL commented on Nov 10, 2021

    @MickL

    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 :(

  17. Elidor00 commented on Dec 9, 2021

    @Elidor00

    I confirm that the problem persists also in angular 12.2

  18. grg-kvl commented on Feb 3, 2022

    @grg-kvl

    The same with version 13. Very annoying.

  19. Mikastark commented on Mar 20, 2024

    @Mikastark

    In v17.3.0, emitting with an output signals in a (@animation.done) will throw an error :

    NG0953: Unexpected emit for destroyed `OutputRef`. The owning directive/component is destroyed
    

    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 safe

  20. zip-fa commented on Apr 24, 2024

    @zip-fa

    In v17.3.0, emitting with an output signals in a (@animation.done) will throw an error :

    NG0953: Unexpected emit for destroyed `OutputRef`. The owning directive/component is destroyed
    

    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 safe

    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

  21. DDtMM commented on Jun 7, 2024

    @DDtMM

    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.

    @Output() readonly displayStateChange = new EventEmitter<OffcanvasComponentState>();
    

    To

    readonly displayStateChange = output<OffcanvasComponentState>();
    
  22. blueiceprj commented on Nov 21, 2024

    @blueiceprj

    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.

  23. sherlock1982 commented on Feb 26, 2025

    @sherlock1982

    This is really annoying after migration to output signals.
    Not mentioned in the docs.
    https://angular.dev/errors/NG0953 leads to 404

    This looks like a warning to me. Can NG0953 be moved to extendedDiagnostics ? Or somehow disabled?

  24. thePunderWoman commented on Aug 21, 2025

    @thePunderWoman
    Contributor

    This issue was closed as obsolete due to the introduction of the new animate.enter and animate.leave APIs. The @angular/animations package is now deprecated and will be removed in Angular v23. We hope you give the new APIs a try.

  25. angular-automatic-lock-bot commented on Sep 21, 2025

    @angular-automatic-lock-bot

    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.

  26. locked and limited conversation to collaborators on Sep 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: animationsfreq2: mediumtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions