首页 文章

Angular 2 ngSwitchCase,OR运算符无法正常工作

提问于
浏览
16

我有多个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>

Output:

if case = 'd' returns Common Code
else if case = 'e' and 'f' returns the Code Default

这里的第二个案例包含多个案例,现在默认情况下 case 'd' 仅在 case 'e' and 'f' 工作且不起作用 .

我在 ngSwitchCase docs中看不到任何多个案例:

https://angular.io/docs/ts/latest/api/common/index/NgSwitchCase-directive.html https://angular.io/docs/ts/latest/api/common/index/NgSwitch-directive . HTML

Doesn't Angular 2 supports the || operator in the ngSwitchCase?

1 回答

  • 45

    如果你评估 'd' || 'e' || 'f' ,结果是 'd' ,当 options 不是 'd' 时,那么它不会't match. You can'这样使用 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>
    

相关问题