2017-10-16 125 views
1

我需要重複的部件的另一組件內,讓我們說:* ngFor沒有內部NG-內容包裝

組分A只接受,它的NG-裏面的內容,類型B.

的一個組成部分做是這樣的:

<component-a> 
    <ng-template ngFor [ngForOf]="items"> 
     <component-B></component-B> 
    </ng-template> 
</component-a> 

或本:

<component-a> 
    <ng-container *ngFor="let item of items"> 
     <component-B></component-B> 
    </ng-container> 
</component-a> 

不工作...

我不能修改組件的一個也不compoenent-B和這些方法並沒有呈現組件-B ...

任何想法如何克服呢?

謝謝。

PS:我可能需要爲每個項目添加多個組件B.

+0

難道你不是指'ng-container'而不是'ng-template'嗎?我寫了差異,因爲我一直搞錯了:https://blog.jonrshar.pe/2017/May/20/angular-ng-elements.html – jonrsharpe

+0

將數組作爲參數傳遞給內部組件,在內部組件的模板。 – omeralper

+0

編輯的問題。我無法修改組件-b。 –

回答

0

我通常會期望的那樣簡單,下面的工作了一句:

<component-a> 
    <component-B *ngFor="let item of items"> 
    </component-B> 
</component-a> 

或者與ngForOf語法,其實並不重要。是否還有更多信息可以幫助您更好地查明問題所在?

+0

上看到預期的結果,但我無法做到這一點,因爲我可能需要爲同一項目添加多個。 –

0

解決!通過使用

ngProjectAs="selector" 

ng-container上的屬性!