angular6动画中如何使用输入参数?

How to use input parameters in angular 6 animation?

在此动画中,我尝试将宽度从 100% 减小到动态起始宽度 "toolWidth"(百分比)。变量 "toolWidth" 可以由用户在应用程序中设置。

动画:

trigger('testAnimation', [
    state('opened', style({
      'width': '100%'
    })),
    state('*', style({
      'width': '{{toolWidth}}%'
    }), {params: {toolWidth: 30}}),
    transition('* => opened', animate('600ms ease-in')),
    transition('opened => *', animate('600ms ease-out'))
  ])

模板:

<div [@testAnimation]="{value: state, params: {toolWidth: newToolWidth}}"></div>

问题:

如果变量 "state" 更改为 "closed",则什么也不会发生。似乎动画不会被新状态 "opened" 触发。 "state"的初始值为"opened"。变量 "newToolWidth" 由用户设置。如果我不使用参数,它工作得很好。

我是不是漏掉了什么?

动画:

trigger('openClose', [
  // ...
  state('open', style({

    backgroundColor: "{{newColor}}"
  }),{params:{ newColor: "yellow"}}),
  state('closed', style({

    backgroundColor: "{{newColor}}"
  }),{params:{ newColor: "red"}}),
  transition('open => closed', [
    animate('0.5s')
  ]),
  transition('closed => open', [
    animate('0.5s')
  ]),
]),

脚本:

ngOnInit() {
    setInterval(() => {
      this.toggle();
    }, 2000);
  }

  dynoColor:string="rgb(0,0,0)";
  isOpen = true;

  toggle() {
    this.isOpen = !this.isOpen;
    let cRed = this.getRandom();
    let cGreen = this.getRandom();
    let cBlue = this.getRandom();
    this.dynoColor = "rgb("+cRed + ", " + cGreen + ", " + cBlue+")";
  }

  getRandom() {
    var x = Math.floor((Math.random() * 255) + 0);
    return x;
  }

模板:

<div [style.width.px]='100px' [style.height.px]='100px' [@openClose]="{value: (isOpen ? 'open' : 'closed'), params: {newColor: dynoColor}}" >

在我的例子中,我随机改变了背景颜色,而且效果很好。

我使用的是 angular 版本 5.2.10

对于其他路过这个问题的人...这就是我在 Angular 7 中制作可重用动画的方式。它也可能适用于 Angular 6:

stackblitz demo here

animations.ts

在单独的文件中创建动画,例如animations.ts。 注意 'params' 行。这些只是默认值,可以在运行时更改:

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

export const slidingDoorAnimation = trigger('slidingDoorAnimation', 
  [
    state('in', 
      style({ width: '{{ inWidth }}', overflow:'hidden'}),
      { params: { inWidth: '250px'}}
    ),
    state('out', 
      style({ width: '{{ outWidth }}'}),
      { params: { outWidth: '*'}}
    ),
    transition('* <=> *',animate('{{ time }}'))
  ]
);

app.component.ts

您现在可以在任何组件中使用此动画,只需从动画文件中导入它即可:

import { Component } from '@angular/core';
import { slidingDoorAnimation } from './animations';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  animations: [slidingDoorAnimation]
})
export class AppComponent  {
  name = 'Angular';
  public slidingDoorValue:string = 'out';

  toggleSlideContent() {
    this.slidingDoorValue = (this.slidingDoorValue == 'in')?'out':'in'; 
  }
}

app.component.html

在html文件中,将参数绑定到public个组件变量。根据动画中定义的状态,变量 slidingDoorValue 设置为 'in' 或 'out'。样式参数是可选的,因为它们具有在动画中定义的默认值。

<div [@slidingDoorAnimation]="{value:slidingDoorValue,params:{inWidth:'100px',time:'1000ms'}}">Hello world</div>