角度动画:如何在模板中动态绑定动画触发器名称?

问题描述 投票:0回答:2

我想知道是否有一种方法可以在模板中动态绑定动画触发器名称。

以下是app.component.html模板中的动画div:

<div [@animationTriggerName]>
  ...
</div>

这是app.module.ts:

...
@NgModule({
  imports:      [...],
  declarations: [ ..., AnimationTriggerNameDirective ],
  bootstrap:    [...]
})

这是app.component.ts:

@Component({
  ...
})
export class AppComponent  {
  ...
  animationTriggerName = 'slideInOut';
}

@Directive({
  selector: '[animationTriggerName]'
})
export class AnimationTriggerNameDirective {
  @Input() animationTriggerName: string;
  constructor() {}
}

我希望能够动态设置变量animationTriggerName。所以,如果我将它设置为myTriggerName,那么在模板中我会渲染它:

<div [@myTriggerName]>
  ...
</div>

因此,触发器名称为myTriggerName的动画将能够运行。

angular angular-directive angular-animations
2个回答
2
投票

在我从这篇文章中学习之后。它看起来像使用多个状态比使用触发器名称更容易,所以我改变我的代码结构如下,下面是您的参考https://angularfirebase.com/lessons/hammerjs-angular-5-animations-for-mobile-gestures-tutorial/的原始帖子

@Component({
  selector: 'hammer-card',
  templateUrl: './hammer-card.component.html',
  styleUrls: ['./hammer-card.component.sass'],
  animations: [
    trigger('cardAnimator', [
      transition('* => wobble', animate(1000, keyframes(kf.wobble))),
      transition('* => swing', animate(1000, keyframes(kf.swing))),
      transition('* => jello', animate(1000, keyframes(kf.jello))),
      transition('* => zoomOutRight', animate(1000, keyframes(kf.zoomOutRight))),
      transition('* => slideOutLeft', animate(1000, keyframes(kf.slideOutLeft))),
      transition('* => rotateOutUpRight', animate(1000, keyframes(kf.rotateOutUpRight))),
      transition('* => flipOutY', animate(1000, keyframes(kf.flipOutY))),
    ])
  ]
})

2
投票

我有类似问题,如发布的问题,到目前为止我使用ngSwitch和ngSwitchCase作为解决方法,因为我在尝试anderror之后未能进行可变绑定。我不认为下面的例子是最佳解决方案,因为如果我想将触发器名称切换为100,这将是乏味的,但它现在适用于我,在运行时更改触发器名称,希望还有其他更好的想法,并希望它有所帮助

<div [ngSwitch]="child.animationName" >
 <input *ngSwitchCase="flyInOut" [@flyInOut]="'in'"  ...>
 <input *ngSwitchCase="fadeIn" [@fadeIn]="'in'"  ...> 
 <input *ngSwitchDefault [@flyRotateInOut]="'in'" ...>
</div> 
© www.soinside.com 2019 - 2024. All rights reserved.