Angular 2 Animation - 布尔触发器?

Ste*_*tes 23 typescript angular2-animation angular

我试图触发绑定到布尔属性的转换,但这似乎没有触发.

这是我的动画触发器的缩减版本

trigger(
  'trueFalseAnimation', [
    transition('* => true', [
      style({backgroundColor: '#00f7ad'}),
      animate('2500ms', style({backgroundColor: '#fff'}))
    ]),
    transition('* => false', [
      style({backgroundColor: '#ff0000'}),
      animate('2500ms', style({backgroundColor: '#fff'}))
    ])
  ]
)
Run Code Online (Sandbox Code Playgroud)

HTML:

<div [@trueFalseAnimation]="model.someProperty">Content here</div>
Run Code Online (Sandbox Code Playgroud)

去测试:

ngOnInit() {

    setTimeout(() => {
        this.model.someProperty = true;
        setTimeOut(() => {
            this.model.someProperty = false;
        }, 5000);    
    }, 1000)
}
Run Code Online (Sandbox Code Playgroud)

someProperty更改时,触发器永远不会发生.

作为一个快速测试我更改了触发器以使用字符串,它的工作原理

trigger(
      'trueFalseAnimation', [
        transition('* => Success', [
          style({backgroundColor: '#00f7ad'}),
          animate('2500ms', style({backgroundColor: '#fff'}))
        ]),
        transition('* => Failed', [
          style({backgroundColor: '#ff0000'}),
          animate('2500ms', style({backgroundColor: '#fff'}))
        ])
      ]
    )
Run Code Online (Sandbox Code Playgroud)

去测试:

ngOnInit() {

    setTimeout(() => {
        this.model.someProperty = "Success";
        setTimeOut(() => {
            this.model.someProperty = "Failed";
        }, 5000);    
    }, 1000)
}
Run Code Online (Sandbox Code Playgroud)

第二个例子很好用

我的问题是

  1. 是否支持boolean作为触发器?
  2. 如果是#1,我做错了什么?

小智 26

  1. 似乎没有.我看到已经为此提出了一个问题(12337),但到目前为止还没有更新.
  2. 另一种选择是使用1和0而不是true和false.

这里看到plunker.

trigger('isVisibleChanged', [
      state('true' , style({ opacity: 1, transform: 'scale(1.0)' })),
      state('false', style({ opacity: 0, transform: 'scale(0.0)'  })),
      transition('1 => 0', animate('300ms')),
      transition('0 => 1', animate('900ms'))
])
Run Code Online (Sandbox Code Playgroud)

  • 对于那些在2017年看到这个答案的人:从Angular 4.0.1开始,你需要使用`state('1',...`和`state('0',...)`而不是`state('true ',...)`和`state('false',...)` (31认同)
  • 来自 2019 年的问候!这已被修复。[布尔值匹配](https://angular.io/api/animations/transition#boolean-value-matching) (2认同)