从指令实例化组件

时间:2016-05-15 17:31:09

标签: angular angular2-directives

我需要实现自定义日期选择器 为此,我有date-picker.directive和select-date.component(对话框窗口)。

我通过html中的属性附加指令并且可以处理事件。通过click事件指令应该调用对话框窗口(它是我们的组件)。

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

但是如何从指令中实例化select-date.component?
如何在DOM中添加新组件?

1 个答案:

答案 0 :(得分:1)

如果你知道组件的类型,你可以将它注入指令

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

如果指令应该能够访问应用它的任何类型的组件,那么您可以使用How to access the Component on a Angular2 Directive中解释的方法(另请参阅此问题https://github.com/angular/angular/issues/8277

相关问题