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

09-10 20:55