我有多个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
文档中看不到任何多个案例:
https://angular.io/docs/ts/latest/api/common/index/NgSwitchCase-directive.html https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive.html
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>