angular - 如何在 Angular 中为 *ngFor 设置动画?

标签 angular transition angular-animations

我需要在填充和显示 ngFor 列表时对其进行动画处理。每个元素都应该有一个过渡,比方说 something like this .

我该怎么做?

最佳答案

它有一些问题,因为 ngFor 做了一些多余的添加/删除,导致项目被动画化,这不应该:

import {Component} from 'angular2/core';
import { Component, Directive, OnDestroy, Input } from 'angular2/core';

@Component({
    selector: 'my-app',
    template: `<div (click)="$event.preventDefault()">
        <button type="button" (click)="pushItem()">Push</button>
        <button type="button" (click)="removeItemLast()">Remove Last</button><br/>
        <button type="button" (click)="unshiftItem()">Unshift</button>
        <button type="button" (click)="removeItemFirst()">Remove First</button><br/>
        <ul>
          <li class="my-animation" *ngFor="#item of items">
            {{item.title}}
          </li>
        </ul>
      </div>`
})
export class AppComponent {
  private count:number = 1;
  public items: Array<any>;
  constructor() { 
    console.clear(); 
    this.items = [];
    this.items.push(this.newItem());
    this.items.push(this.newItem());
    }
    pushItem() {
        this.items.push(this.newItem());
    },
    removeItemLast() {
      if(this.items.length > 0) this.items.splice(this.items.length - 1, 1);
    },
    unshiftItem() {
        this.items.unshift(this.newItem());
    },
    removeItemFirst() {
      if(this.items.length > 0) this.items.splice(0, 1);
    },
    newItem() {
      return {title: 'Item' + this.count++};
    }

}
@keyframes MyAnimation {
  0% {
    padding-left: 100px;
  }
  100% {
    padding-left: 0px;
  } 
}

.my-animation {
  animation: MyAnimation 1s;
}

Plunker example (RC.x) 来自 https://github.com/angular/angular/issues/7239演示了这个问题。

更新

这个问题很久以前就解决了

工作 Demo on stackblitz

关于angular - 如何在 Angular 中为 *ngFor 设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217314/

相关文章:

Angular Cli 警告 : Option "entryComponent" is deprecated: Since version 9. 0.0 与 Ivy,不再需要 entryComponents

css - 带有滚动的淡入淡出卡片 - Angular (我接受图书馆的建议)

html - 当我将鼠标移到链接上时,平滑淡出消失了

c# - 从渐变上的某个位置显示颜色

javascript - 在 D3.js 中过渡旭日形

angular-material - 使用 routerLink 时,角度 Material 选项卡动画会中断

angular - 向宿主元素添加 Angular 动画

angular - Angular 函数调用时出错

重新实例化数据时,Angular CDK 拖放列表会中断

javascript - 如何在 Angular 组件之间调用动态函数