gpt4 book ai didi

Angular 2 ngSwitchCase,或运算符不工作

转载 作者:太空狗 更新时间:2023-10-29 16:54:05 29 4
gpt4 key购买 nike

我有多个 switch 语句,但对于某些情况,我需要常见的情况。所以,我正在尝试

OR 运算符 => ||

例子:

        <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' 和 '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>

关于Angular 2 ngSwitchCase,或运算符不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775796/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com