2017-08-02 24 views
6

में elseif है मैंवहाँ कोणीय 4

<ng-template [ngIf]="xyz== 1">First</ng-template> 
<ng-template [ngIf]="pqr == 2">Second</ng-template> 
<ng-template [ngIf]="abc == 3">Third</ng-template> 

ऊपर बयान में एकाधिक शर्तों सच हो सकता है बयान की एक संख्या है।

लेकिन मैं क्या चाहते हैं, है पहले बयान के लिए पहली जांच करता है, तो सच तो प्रदर्शित करने और बाकी

छोड़ झूठी है, तो दूसरे के लिए जाँच और इतने

यह कैसे प्राप्त करने के लिए पर?

+3

संभावित डुप्लिकेट [? कैसे उपयोग करने के लिए \ * ngIf कोणीय 4 में बाकी] (https://stackoverflow.com/questions/43006550/how-to- उपयोग-एनजीआईफ़-अन्य-इन-कोणीय -4) – Supamiu

+1

@ सुपामीयू प्रश्न पूरी तरह से अलग है, कृपया वर्णन मुझे यह नहीं चाहिए कि अगर और एकाधिक कंडिशन सत्य हो सकते हैं लेकिन मैं पहले को प्रदर्शित करना चाहता हूं सच है और बाकी को छोड़ दें –

+0

@ सुपामी मुझे अगर अन्यथा चाहिए तो क्या आप सुझाव दे सकते हैं कि क्या कोई अन्य समाधान आयन –

उत्तर

2

आप उपयोग करने का प्रयास कर सकते हैं ngIf निर्देश की तरह:

<ng-template [ngIf]="xyz == 1" [ngIfElse]="second">First</ng-template> 
<ng-template #second> 
    <ng-template [ngIf]="pqr == 2" [ngIfElse]="third">Second</ng-template> 
</ng-template> 
<ng-template #third> 
    <ng-template [ngIf]="abc == 3">Third</ng-template> 
</ng-template> 

ng-container के साथ के रूप में दिखाई देगा इस प्रकार है:

<ng-container *ngIf="xyz == 1; else second">First</ng-container> 
<ng-template #second> 
    <ng-container *ngIf="pqr == 2; else third">Second</ng-container> 
</ng-template> 
<ng-template #third> 
    <ng-container *ngIf="abc == 3">Third</ng-container> 
</ng-template> 

लेकिन मैं निम्नलिखित समाधान की पेशकश कर सकते हैं पाश बयान के लिए उपयोग करना चाहते हैं:

<ng-container *ngTemplateOutlet="next; context: { $implicit: 0 }"></ng-container> 

<ng-template #next let-i> 
    <ng-container *ngIf="conditions[i]"> 
    <ng-container *ngIf="conditions[i] && conditions[i].condition(); else shift">{{ conditions[i].result }}</ng-container> 
     <ng-template #shift > 
     <ng-container *ngTemplateOutlet="next; context: { $implicit: i + 1 }"></ng-container> 
     </ng-template> 
    </ng-container> 
</ng-template> 

जहां conditions

conditions = [ 
    { 
    condition:() => this.xyz === 1, 
    result: 'First' 
    }, 
    { 
    condition:() => this.pqr === 2, 
    result: 'Second' 
    }, 
    { 
    condition:() => this.abc === 3, 
    result: 'Third' 
    } 
]; 

Plunker Example

+0

अगर मैं इसे मैन्युअल रूप से लिखता हूं लेकिन इसमें कई कथन हो सकते हैं और मैं इसे लूप के साथ पॉप्युलेट कर रहा हूं। पहले लिखना, दूसरा एक प्रोब –

+0

होगा, शायद मैं अब "अन्य 1" "अन्य 2" कर सकता हूं। क्या कोई अन्य कुशल तरीका है जिसके बारे में आप सोच सकते हैं? –

+0

क्या आप मुझे दिखा सकते हैं कि लूप कैसा दिखता है? – yurzui

0

क्यों सिर्फ ऐसा नहीं घटक में अधिक काम:

<ng-template [ngIf]="status == 'first'">First</ng-template> 
<ng-template [ngIf]="status == 'second'">Second</ng-template> 
<ng-template [ngIf]="status == 'third'">Third</ng-template> 
घटक कोड में

:

status string; 
if (xyz == 1) 
    status = 'first'; 
else if (pqr == 2) 
    status = 'second'; 
else if (abc == 3) 
    status = 'third'; 
की
+0

मैं कोणीय में प्रतिक्रियाशील रूपों का उपयोग कर रहा हूं, समस्या तब आती है जब मेरे पास एकाधिक सत्यापन होते हैं और मैं पहले विफल सत्यापन –

+0

के लिए त्रुटि संदेश दिखाना चाहता हूं। मेरे पास उन सत्यापन की वापसी त्रुटि तारों का नाम है firstName.errors.textLength और एक सरणी सत्यापनकर्ताओं के अनुरूप दिखाने के लिए त्रुटि संदेश शामिल हैं –