2017-05-04 17 views
7

Birden çok anahtar ifadem var, ancak bazı durumlarda ortak duruma ihtiyacım var. Yani,Köşeli 2 ngSwitchCase, VEYA işleci çalışmıyor

OR operator => ||

Örnek çalışıyorum:

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

Çıktı: İşte

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

ikinci son durum varsayılan olarak artık birden durumlarda oluşur ve case 'd' sadece çalışıyor ve case 'e' and 'f' için çalışmıyor.

Ben ngSwitchCase dokümanlar içindeki herhangi çoklu durum göremiyorum:

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

Eğik 2 ngSwitchCase içinde || operatörü destekler etmiyor mu?

cevap

18

Eğer sonuç 'd' ve options'd' değilken, o zaman uymuyor 'd' || 'e' || 'f' değerlendirmek durumunda. ngSwitchCase'u bu şekilde kullanamazsınız.

Bu çalışacak:

<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

Vay! “Açgözlülük Tanrısı” dırsın: D '' [ngSwitch] 'inin gerçek koşullara sahip olduğunu düşünmüyordum. ;) Bunun için teşekkürler. Kafamı saatlerce çiziyordum. – PaladiN

+1

Teşekkürler, rica ederim. Sorununuzu düzelttiğini duyduğuma sevindim :) –

+3

İşleri! Anahtar nokta: [ngSwitch] = "true", tüm "SwitchCase koşulu" etiketlerinin değerlendirilmesine izin verecek – ObjectiveTC