我有三個組件,其中componentC
作爲componentB
的內容出現,並在其構造函數中獲得componentB
DI'ed。DI和transclusion
@Component({
selector: 'componentA',
template: `
<componentB>
<componentC></componentB>
<ng-content></ng-content>
</componentB>`
....
export class ComponentC {
constructor(private cmpB: ComponentB)
現在我用componentA
從另一個組件,並希望通過我把在componentA
的模板<ng-content>
使用transclusion提供額外componentC
s到它:
<componentA>
<componentC></componentC>
</componentA>
這會觸發No provider for ComponentB
。使用<ng-content>
,我定義內容應該出現在<componentB>
之內,因此DI應該能夠找到作爲插入點的父對象的ComponentB
對象。
這種情況可能嗎? Angular會直接在目標位置創建額外的ComponentC
,還是首先在標記處移動它?有沒有辦法配置供應商來做到這一點?
有關跨越不改變組件層次結構的好處,但只是移動DOM的東西。 會有另一種解決方案使用模板和組件工廠來實例化內容作爲'componentB'的子項嗎? – achimha