2016-05-15 53 views
3

我需要實現自定義日期選擇器。
爲此我有date-picker.directive和select-date.component(對話窗口)。從指令實例化組件

我通過html中的屬性附加指令,並可以處理事件。通過點擊事件指令應該調用對話窗口(這是我們的組件)。

<button class="btn" type="button" date-picker> 

但是我怎樣才能從指令實例化select-date.component?
如何將新組件添加到DOM?

回答

1

如果你知道組件的類型,你可以把它注射到指令

export class DatePickerDirective { 
    constructor(private selectDateComponent:SelectDateComponent) {} 
} 

如果它被應用,那麼你可以使用的方法解釋該指令應該能夠訪問任何類型的組件在How to access the Component on a Angular2 Directive(另見本期https://github.com/angular/angular/issues/8277

+0

GünterZöchbauer,您的解決方案有助於訪問已存在的組件。是否有可能創建一個組件的新實例並將其添加到DOM? – rnofenko

+0

查看http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468舉例 –

+0

GünterZöchbauer,感謝您的幫助,但您的榜樣演示如何從component_創建_component。當我想從directive_找到_component的解決方案時。 – rnofenko