正确的 ngSwitchCase 不会使段落出现



我有一个按钮,它将数组中的两个字符串之一分配给变量。

然后,使用[ngSwitch],我只想显示一段。

这些段落似乎都没有出现,我只是在每次点击按钮时收到随机警报。

哦,这个组件是在主组件上调用的。


animations.component.html

<button (click)="chooseAnimation()">button</button>
<div [ngSwitch]="animation">
<div *ngSwitchCase="splittingImage">
<p> splitting </p>  
</div>
<div *ngSwitchCase="zoomAndBlur">
<p> zooming </p> 
</div>
</div>

animations.component.ts


export class AnimationsComponent implements OnInit {
animation;
animations = ['zoomAndBlur','splittingImage'];
ngOnInit(){
}
chooseAnimation(){
this.animation = this.animations[Math.floor(Math.random() * this.animations.length)];
alert(this.animation); //this works fine and alerts zoomAndBlur or splittingImage. 
}
constructor() {
}

尝试将*switchCase值包装成单引号。

<div *ngSwitchCase="'splittingImage'"><div *ngSwitchCase="'zoomAndBlur'">

最新更新