2016-07-29 41 views
1

讓我們說我有以下的基本組成部分有兩個插槽:多插槽轉換與Angular 2組件一起工作嗎?

// my-component.component.ts 
@Component({ 
    selector: 'my-component', 
    template: ` 
<div class="my-component"> 
    <div> 
    Title: 
    <ng-content select="my-component-title"></ng-content> 
    </div> 
    <div> 
    Content: 
    <ng-content select="my-component-content"></ng-content> 
    </div> 
</div>` 
}) 

進入第二插槽(「我的成分含量」)我想插入規則的角2組件...

<div class="app"> 
    <my-component> 
    <div class="my-component"> 
     <div> 
     Title: 
     <my-component-title> 
      This is the Component title! 
     </my-component-title> 
     </div> 
     <div> 
     Content: 
     <my-component-content> 
      <some-regular-angular2-component></some-regular-angular2-component> 
     </my-component-content> 
     </div> 
    </div> 
    </my-component> 
</div> 

在哪裏一些-常規angular2成分'是一些角2成分的選擇器...

@Component({ 
    selector:'some-regular-angular2-component' 
})' 

問題是,「一些-常規angular2-COMPONE nt'永遠不會被轉換到ng-content的第二個槽中......只有普通的HTML適用於我...我試圖將'some-regular-angular2-component'設置爲父組件的[directive],但Angular 2組件無法在ng內容中識別...或者這對你有用嗎?

+0

你可以發表一個你認爲應該工作的樣本plunk或jsfiddle嗎?你的父'@ Component'裝飾器的'directives'中列出了'some-regular-angular2-component'類嗎?那必須在那裏。 –

回答

0

您需要將<ng-content></ng-content>添加到<my-component-content>的視圖(模板)中如果<my-component-content>不支持跨接,它的子將不會顯示。代碼中的<ng-content>標籤僅適用於直接匹配的元素,但不適用於其子級。

0

只是爲了回答我的問題,是的,角度2的transclusion支持插入角度2的組件。代碼中我有愚蠢的錯字。

0

是的。它現在被稱爲內容投影。請參閱具有多個插槽的模式對話框的this example

相關問題