I'm trying to trigger a transition bound to a boolean property, but this doesn't seem to fire.
Here is a cut down version of my animation trigger
trigger(
'trueFalseAnimation', [
transition('* => true', [
style({backgroundColor: '#00f7ad'}),
animate('2500ms', style({backgroundColor: '#fff'}))
]),
transition('* => false', [
style({backgroundColor: '#ff0000'}),
animate('2500ms', style({backgroundColor: '#fff'}))
])
]
)
HTML:
<div [@trueFalseAnimation]="model.someProperty">Content here</div>
To test:
ngOnInit() {
setTimeout(() => {
this.model.someProperty = true;
setTimeOut(() => {
this.model.someProperty = false;
}, 5000);
}, 1000)
}
The trigger never happens when the someProperty
changes.
As a quick test I changed the trigger to use a string and it works
trigger(
'trueFalseAnimation', [
transition('* => Success', [
style({backgroundColor: '#00f7ad'}),
animate('2500ms', style({backgroundColor: '#fff'}))
]),
transition('* => Failed', [
style({backgroundColor: '#ff0000'}),
animate('2500ms', style({backgroundColor: '#fff'}))
])
]
)
To test:
ngOnInit() {
setTimeout(() => {
this.model.someProperty = "Success";
setTimeOut(() => {
this.model.someProperty = "Failed";
}, 5000);
}, 1000)
}
The second example works just fine
My questions are
- Are boolean supported as triggers?
- If yes to #1 what am I doing wrong?
See the plunker from here.
I'm having the same issue. Not sure if boolean are supported as triggers, but the workaround I found was to define a string property with a getter to return the boolean value as string. Something like this:
Then you should bind your animation to that
somePropertyStr
property.Once again, this is an ugly workaround, best thing would be able to use the boolean value.
A state is defined as being a string, so we need to stick to that.
The simplest - but ickiest - way based on your code is this
But this is pretty awful, so this is probably better
The best advice here would be to use a state that corresponds to what it really means. What does true and false really mean in this context anyway?
I considered making a pipe to convert a boolean, but the only benefit of that would be to make sure you're being consistent with your state strings.