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

问题描述 投票:16回答:1

我有多个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运算符吗?

angular switch-statement ng-switch or-operator
1个回答
45
投票

如果你评估'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>
© www.soinside.com 2019 - 2024. All rights reserved.