2016-08-07 54 views
2

我用NG-內容與* ngIf一個組件內有一個奇怪的行爲,請參閱本plunkr:http://plnkr.co/edit/BZim5lMhihuyAkDPfzZ6?p=preview與內容投影* ngIf不能正常工作

它CheckBox組件顯示在左邊或右邊的標籤複選框取決於right屬性。

@Component({ 
    selector: "em-checkbox", 
    template: ` 
<div class="form-group"> 
    <div class="checkbox"> 
    <label *ngIf="right"> 
     <input type="checkbox"> 
     <ng-content></ng-content> 
    </label> 
    <label *ngIf="!right"> 
     <ng-content></ng-content> 
     <input type="checkbox"> 
    </label> 
    </div> 
</div> 
    ` 
}) 
export class EmCheckbox { 
    @Input() right = false; 
} 

它只適用於第一個ng內容,但不是第二個,我不能把標籤放在右邊。

回答

1

ng-content只能使用一次(請參閱https://github.com/angular/angular/issues/9173)。

但我你並不真的需要使用它的感覺兩次只是調整(實際上,簡化)模板:

<div class="form-group"> 
    <div class="checkbox"> 
    <label> 
     <input *ngIf="right" type="checkbox"> 
     <ng-content></ng-content> 
     <input *ngIf="!right" type="checkbox"> 
    </label> 
    </div> 
</div> 
+1

怎麼樣這種情況下:我有分量「佈局」,如果變量'userLoggedIn == TRUE'我想提出'

'如果沒有,我想提出:'
' - 怎麼辦呢? –

+0

好問題,我也有非空元素,我想有條件地顯示每個與內部的投影內容,但它似乎像角不支持此用例 – Ronin