我有多个switch语句,但在某些情况下,我需要常见的情况。所以,我正在尝试OR operator => ||
例:
<ng-container [ngSwitch]="options">
<ng-container *ngSwitchCase="'a'">Code A</ng-container>
<ng-container *ngSwitchCase="'b'">Code B</ng-container>
<ng-container *ngSwitchCase="'c'">Code C</ng-container>
<ng-container *ngSwitchCase="'d' || 'e' || 'f'">Common Code</ng-container>
<ng-container *ngSwitchDefault>Code Default</ng-container>
</ng-container>
输出:
if case = 'd' returns Common Code
else if case = 'e' and 'f' returns the Code Default
在这里,倒数第二种情况由多种情况组成,现在默认情况下
case 'd'
仅适用于case 'e' and 'f'
,而不适用。我在
ngSwitchCase
文档中看不到任何重复的情况:Angular 2在
||
中不支持ngSwitchCase
运算符吗? 最佳答案
如果您评估'd' || 'e' || 'f'
的结果是'd'
,而当options
不是'd'
时,则不匹配。您不能以这种方式使用ngSwitchCase
。
这将工作:
<ng-container [ngSwitch]="true">
<ng-container *ngSwitchCase="options === 'a'">Code A</ng-container>
<ng-container *ngSwitchCase="options === 'b'">Code B</ng-container>
<ng-container *ngSwitchCase="options === 'c'">Code C</ng-container>
<ng-container *ngSwitchCase="options === 'd' || options === 'e' || options === 'f'">Common Code</ng-container>
<ng-container *ngSwitchDefault>Code Default</ng-container>
</ng-container>