Angular2:你如何并行运行2个动画?

AJ *_* X. 9 typescript angular-ng-if angular

我有一个扩展/收缩的容器.容器内部有一个元素,当容器膨胀时应该淡入,当容器收缩时应该淡出.

我的问题

  • 当容器展开时,两个元素的动画都有效.
  • 当容器缩小时,只有容器动画才有效.
  • 如果我删除容器扩展动画,则淡入/淡出动画将按预期工作.

如何在扩展/收缩条件下并行执行所有动画?

注意使用ngIf.这是故意的,因为它会破坏动画序列末尾的元素.

这是我当前状态的一个傻瓜:https://embed.plnkr.co/TXYoGf9QpErWmlRvQF9Z/

组件类:

export class App {
  expanded = true;

  toggleExpandedState() {
    this.expanded = !this.expanded;
  }

  constructor() {
  }
}
Run Code Online (Sandbox Code Playgroud)

模板:

<div class="container" [@expansionTrigger]="expanded">
  <div class="constant-item"></div>
  <div class="fade-item" [@stateAnimation] *ngIf="expanded"></div>
</div>

<button (click)="toggleExpandedState()">Toggle Fade</button>
Run Code Online (Sandbox Code Playgroud)

和组件动画:

  trigger('expansionTrigger', [
    state('1', style({
      width: '250px'
    })),
    state('0', style({
      width: '160px'
    })),
    transition('0 => 1', animate('200ms ease-in')),
    transition('1 => 0', animate('200ms ease-out'))
  ]),
  trigger('stateAnimation', [
    transition(':enter', [
      style({
        opacity: 0
      }),
      animate('200ms 350ms ease-in', style({
        opacity: 1
      }))
    ]),
    transition(':leave', [
      style({
        opacity: 1
      })
      animate('1s', style({
        opacity: 0
      }))
    ])
  ])
Run Code Online (Sandbox Code Playgroud)

AJ *_* X. 3

为了避免其他人的挫败感,看起来这是 Angular 4 的一个错误。

目前 Github 上有两个悬而未决的问题,它们似乎密切相关:

https://github.com/angular/angular/issues/15798

https://github.com/angular/angular/issues/15753

更新

根据 github 问题,动画错误不会像 Angular2 中那样被修复。相反query()animateChild()正在推出。新的动画功能从 4.2.0-rc2 开始可用。

总之:

组件定义

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
      <button (click)="toggle()">Toggle</button>
      <div *ngIf="show" @ngIfAnimation>
        <h3 @easeInOut>I'm inside ngIf</h3>
      </div>
    </div>
  `,
animations: [
  trigger('ngIfAnimation', [
    transition(':enter, :leave', [
      query('@*', animateChild())
    ])
  ])
  trigger('easeInOut', [
    transition('void => *', [
        style({
            opacity: 0
        }),
        animate("1s ease-in-out", style({
            opacity: 1
        }))
    ]),
    transition('* => void', [
        style({
            opacity: 1
        }),
        animate("1s ease-in-out", style({
            opacity: 0
        }))
    ])
  ])
]]

})
export class App {
  name:string;
  show:boolean = false;

  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }

  toggle() {
    this.show = !this.show;
  }
}
Run Code Online (Sandbox Code Playgroud)

这里有一个工作示例:https ://embed.plnkr.co/RJgAunJfibBKNFt0DCZS/