How can I animate list items when they are added or removed in Angular

0 votes
Can i know How can I animate list items when they are added or removed in Angular?
Mar 11 in Angular by Ashutosh
• 23,030 points
50 views

1 answer to this question.

0 votes

In Angular, you can animate list items when they are added or removed using Angular Animations (@angular/animations) with @trigger.

1. Import Required Modules (in app.module.ts)

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({

  imports: [BrowserAnimationsModule],

})

export class AppModule {}

2. Define Animation in Component (list.component.ts)

import { Component } from '@angular/core';

import { trigger, transition, style, animate } from '@angular/animations';

@Component({

  selector: 'app-list',

  templateUrl: './list.component.html',

  animations: [

    trigger('listAnimation', [

      transition(':enter', [

        style({ opacity: 0, transform: 'translateY(-10px)' }),

        animate('300ms ease-out', style({ opacity: 1, transform: 'translateY(0)' }))

      ]),

      transition(':leave', [

        animate('300ms ease-in', style({ opacity: 0, transform: 'translateY(10px)' }))

      ])

    ])

  ]

})

export class ListComponent {

  items: string[] = [];

  addItem() { this.items.push(`Item ${this.items.length + 1}`); }

  removeItem(index: number) { this.items.splice(index, 1); }

}

3. Apply Animation in Template (list.component.html)

<button (click)="addItem()">Add Item</button>

<ul>

  <li *ngFor="let item of items; let i = index" @listAnimation>

    {{ item }} <button (click)="removeItem(i)">Remove</button>

  </li>

</ul>

answered Mar 11 by Tanmay

Related Questions In Angular

0 votes
1 answer

How can I strip HTML tags from a string in ASP.NET?

Hello @Kartik, If it is just stripping all HTML tags ...READ MORE

answered Jul 23, 2020 in Angular by Niroj
• 82,840 points
2,038 views
0 votes
1 answer

How to know tools and bundlers after create a new workspace or a project in angular?

Hello @sajal, When you create projects and workspaces ...READ MORE

answered Aug 6, 2020 in Angular by Niroj
• 82,840 points
1,174 views
0 votes
1 answer

How do I include a JavaScript script file in Angular and call a function from that script?

Hello @kartik, Refer the scripts inside the angular-cli.json (angular.json when using ...READ MORE

answered Sep 8, 2020 in Angular by Niroj
• 82,840 points
14,398 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

What is the difference between Node.js and Express.js?

Feature Node.js Express.js Definition A runtime environment for executing JavaScript outside ...READ MORE

answered Mar 11 in Node-js by Tanmay
54 views
0 votes
1 answer

How can I configure lazy loading for Angular modules?

To configure lazy loading in Angular, you ...READ MORE

answered Dec 12, 2024 in Angular by Navya
124 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP