2017-05-04 4 views
7

여러 switch 문이 있지만 어떤 경우에는 일반적인 경우가 필요합니다. 그래서, 나는각도 2 ngSwitchCase, OR 연산자가 작동하지 않습니다.

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

이 각도 2는 ngSwitchCase에서 || 연산자를 지원하지 않는다?

답변

18

당신이 결과가 'd'하고 options'd'이없는 경우, 다음이 일치하지 않는 'd' || 'e' || 'f'을 평가합니다. 그런 식으로 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> 
+2

와우! 당신은 '각도의 신'입니다 : D 나는 [ngSwitch]가 진정한 조건을 가져야한다고 생각하지 않았습니다. ;)이 방법을 주셔서 감사합니다. 나는 몇 시간 동안 머리를 긁적 거리고 있었다. – PaladiN

+1

고마워, 천만에. 다행히 문제를 해결할 수 있습니다. –

+3

Works! 요점 : [ngSwitch] = "true"로 설정하면 모든 "SwitchCase condition"태그가 평가됩니다. – ObjectiveTC

관련 문제