Angular 2 - 将组件动画的延迟传递为输入参数

时间:2016-10-20 04:38:51

标签: angular angular2-animation

我想从html中传递组件动画的延迟,例如:

HTML:

<circles[delay]="'10000ms'"></circles> 

TS:

@Component({
   selector: 'circles',
   templateUrl: 'app/landing-page/subcomponents/circles.component.html',
   styleUrls: ['app/landing-page/subcomponents/circles.component.css'],
    animations: [
        trigger('flyIn', [
            state('in', style({ transform: 'translateY(0)', opacity: 1 })),
            transition('void => *', [
                style({ transform: 'translateY(-100%)', opacity: 0 }),
                animate("1000ms" + this.delay)
            ])
        ])
    ]
})

export class CirclesComponent {
   @Input() private delay: string; 

但是当我这样做时会出现这个错误:

  

(SystemJS)无法读取未定义(...)

的属性“延迟”

如何在不导致此错误的情况下将延迟传递给html中的组件?

2 个答案:

答案 0 :(得分:1)

您正试图在this中使用this.delay来引用当前的类,但是您正在课外进行此操作。请注意,@Component()函数在声明class CirclesComponent

之前执行

这不是很优雅,但是当你想设置延迟时,你可以在window对象上设置一个属性

window.custom = {delay:'1000ms'}

然后在你的动画中,你可以用`window.custom来访问它? window.custom.delay:

animate("1000ms" + (window.custom? window.custom.delay : ""))

答案 1 :(得分:0)

我可能会迟到一点,但这可能会对其他人有所帮助。

onInputChange(event:any){
        console.log("This is emitted as the thumb slides");   
}

将其加载到您的组件中

// Define your animations
export const fadeInDelay =
  trigger('fadeInDelay', [
    transition('void => *', [
        style({ opacity: '0' }),
        animate('250ms {{delay}}ms ease-in')
      ],
      { params: { delay: 200 } } //Fallback value; else it could crash when no delay is passed
    )
  ]);

然后您可以像这样在模板中使用它,并控制每个动画的延迟:

@Component({
  animations: [fadeInDelay]
})
export class Component {
  ...
}

别忘了传递一个值,否则它将不起作用。 我希望这会有所帮助!