2016-06-17 9 views
11

मैं समझने के लिए एक बनाने का तरीका अगर दिखाने के लिए जब एक ng-content खाली है कोशिश कर रहा हूँ है।एनजी-सामग्री सामग्री या खाली Angular2

<div #contentWrapper [hidden]="isOpen"> 
    <ng-content ></ng-content> 
</div> 

<span *ngIf="contentWrapper.childNodes.length == 0"> 
    <p>Display this if ng-content is empty!</p> 
</span> 

मुझे लगता है कि एक का उपयोग करने के लिए एक प्रदर्शन डेटा दिखाने के लिए जब सामग्री रिक्त है लेकिन कोशिश की है, भले ही जानकारी खाली है <span> टैग प्रदर्शित नहीं करता है

धन्यवाद, हमेशा आपकी मदद के लिए आभारी ।

उत्तर

11

उपयोग childNodes के बजाय children। कोणीय *ngIf* which are counted by के लिए टिप्पणी नोड्स बनाता childNodes`

<div #contentWrapper [hidden]="isOpen"> 
    <ng-content ></ng-content> 
</div> 

<span *ngIf="contentWrapper.children.length == 0"> 
    <p>Display this if ng-content is empty!</p> 
</span> 

Plunker example

+0

धन्यवाद मेट, आपका जवाब इतना बेहतर – rodboc

+0

काम करता है मुझे लगता है अगर ngIf इस में कोई आवरण पर ही है – Ced

+0

@Ced, कि काम नहीं करेगा मामले आप '@ViewChild ('contentWrapper') आवरण की तरह कुछ का उपयोग करने के: ElementRef;' और '* ngIf ="? आवरण .nativeElement .children .length? "' –