9

I have an Angular 4 component with a list of items. I followed the Angular documentation (https://angular.io/api/animations/animation) on setting up Enter and Leave animation when item is added or removed. But the same animation is played on all existing items in the list when the view is loaded. Is there a way to turn off animation on the initial view load but re-enable it after view is loaded?

Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
JHC
  • 91
  • 1
  • 2

2 Answers2

5

You can define a dedicated animation state for added components.

Then you will have 3 transitions

  1. void => * // Initial load - no animations
  2. void => added // Animation for added components
  3. * => void // Animation for removal of component
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
4

Just in case someone is looking for actual answer - if you have animation with :enter trigger, it can be done with @.disabled attribute like so:

<div @someAnimation [@.disabled]="animationDisabled"></div>

And then in component you just need to change the animationDisabled property to true after view is initialized:

@Component({
    // ...
})
export class SomeComponent implements AfterViewInit {
   
    animationDisabled: boolean = true;

    ngAfterViewInit(): void {
        this.animationDisabled = false;
    }

}

Now animation won't run on component init but will still work during component's lifetime

ThaFog
  • 493
  • 3
  • 15