如何在角度6动画中使用输入参数?

时间:2018-06-11 21:21:39

标签: angular angular-animations

在这个动画中,我尝试将宽度从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”,则没有任何反应。似乎动画不会被新状态“打开”触发。 “州”的初始值是“打开”。变量“newToolWidth”由用户设置。如果我不使用参数,它可以很好地工作。

我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

对于其他绕过此问题的人...这就是我在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文件中,将参数绑定到公共组件变量。根据动画中定义的状态,将变量slideDoorValue设置为“输入”或“输出”。样式的参数是可选的,因为它们具有在动画中定义的默认值。

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

答案 1 :(得分:-1)

动画:

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}}" >

就我而言,我是在随机更改背景色,并且效果很好。

我使用的是角度版本 5.2.10