Skip to content

animate.leave breaks @for reactivity #63544

Closed
Closed

Description

@sigve10

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

When using animate.leave within @for, it seems that @for may fail to render items it is supposed to render.

The provided example includes a @for that displays a sliced window of a list with a maximum of 3 elements. Removing the middle item causes the @for to only display a window of 2 elements, even in scenarios where it should still display 3. Further interaction might also inconsistently reduce the number of shown elements to 1, or even 0, or it may rarely return the number of displayed items to 3. I have yet to find any consistent trigger besides removing the second to last item of a list like this.

Playing around with the list will inevitably lead to NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-horm4s7k

Please provide the exception or error you saw

Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.2.1
Node: 20.19.1
Package Manager: npm 10.8.2
OS: linux x64
    

Angular: 20.2.3
... common, compiler, compiler-cli, core

Package                      Version
------------------------------------
@angular-devkit/architect    0.2002.1
@angular-devkit/core         20.2.1
@angular-devkit/schematics   20.2.1
@angular/build               20.2.1
@angular/cdk                 20.2.1
@angular/cli                 20.2.1
@schematics/angular          20.2.1
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.1

Anything else?

No response

Activity

  1. added
    area: core
    core: animations
    on Sep 2, 2025
  2. modified the milestone: needsTriage on Sep 2, 2025
  3. thePunderWoman commented on Sep 4, 2025

    @thePunderWoman
    Contributor

    Thanks for reporting this. We'll look into what's happening.

  4. imaksp commented on Sep 9, 2025

    @imaksp
    Contributor

    FYI, I could solve this by using track $index instead of track item in your reproduction. with $track item list with animate.leave behaves in unexpected way.

    I also faced one issue with animation timing when using @for inside @if block with class based animate.leave but could not understand to cause fully so not created an issue (solved it by using event version.), with class version I think it was not removing old elements from DOM, even if we update signal array (used in @for) using .set.

  5. sigve10 commented on Sep 9, 2025

    @sigve10
    Author

    FYI, I could solve this by using track $index instead of track item in your reproduction. with $track item list with animate.leave behaves in unexpected way.

    Tracking the index doesn't "solve" the issue. All it does is prevent the leave animation from running, as shifting the list doesn't change the $index, meaning the @for does not see the shifting as an actual change.

  6. imaksp commented on Sep 9, 2025

    @imaksp
    Contributor

    Hi, you are right, I did not check code in detail, bug is related to element removal on leave. list displays correctly if we add leave func like this:

     leavingFn(event: AnimationCallbackEvent) {
        event.target.classList.toggle('on-leave');
        event.animationComplete();
     }

    but this does not animate items correctly, and it doesn't work correctly if we call animationComplete after some delay.

  7. sigve10 commented on Sep 9, 2025

    @sigve10
    Author

    @imaksp If you are looking for a temporary solution, I have updated the stackblitz with a workaround I found.

  8. thePunderWoman commented on Sep 11, 2025

    @thePunderWoman
    Contributor

    so it looks like what's happening is when the second to last item is removed, this triggers an update to the last item's index, which causes it to get "moved", which is a remove and add. This is triggering the remove animation, which is what's causing this. We'll see what we can figure out to resolve this.

  9. added 4 commits that reference this issue on Sep 11, 2025
    ab1117a
    00a2f00
    124f1c5
    9bec3d5
  10. added a commit that references this issue on Sep 12, 2025
    3ec8a5c
  11. added a commit that references this issue on Sep 18, 2025
    96c894f
  12. angular-automatic-lock-bot commented on Oct 13, 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.

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

Metadata

Metadata

Labels

area: corecore: animations

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions