Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular animate on property change

I have a custom navigable panel for a list of items. The panel contains one databound component, surrounded by two buttons for navigating to previous and next data. The databound component displays the currently active data. Now, I wish to show some animation when the data object changes. I am new with angular animation and finding it hard to find an example that suits my scenario. How can I trigger an animation when an @Input() property value changes?

like image 252
Victor Mukherjee Avatar asked Oct 30 '25 13:10

Victor Mukherjee


1 Answers

If you're not talking about router animations, I think you'll have to maintain an animation state using an @Input() setter and the animations done function. Example here https://stackblitz.com/edit/angular-lmmixg

Child component ts:

  @Input() set data(data: any) {
    this.dataState = 'entering';
    this._data = data;
  }

  get data() { return this._data };

  _data: any;

  dataState: 'entering' | 'done' = 'done';

Child component html:

<div [@dataChange]="dataState" (@dataChange.done)="dataState = 'done'">{{ data }}</div>

Animation ts:

export const dataChange: AnimationTriggerMetadata = trigger('dataChange', [
    transition('done => entering', [
        style({
            'margin-left': '90%'
        }),
        animate('200ms ease',
            style({ 'margin-left': '*' }))
    ]),
]);
like image 170
Frederick Avatar answered Nov 01 '25 06:11

Frederick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!