3

I need to implement custom date picker.
For this I have date-picker.directive and select-date.component (dialog window).

I attach directive through attribute in html and can process events. By click event directive should call dialog window (it's our component).

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

But how can I instantiate select-date.component from a directive?
How to add a new component into DOM?

rnofenko
  • 9,198
  • 2
  • 46
  • 56

1 Answers1

1

If you know the type of the component you can just inject it into the directive

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

If the directive should be able to access any type of component where it is applied then you can use the method explained in How to access the Component on a Angular2 Directive (see also this issue https://github.com/angular/angular/issues/8277)

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Günter Zöchbauer, your solution helps to get access for existed component. Is it possible to create a new instance of component and add it to DOM? – rnofenko May 16 '16 at 02:17
  • See http://stackoverflow.com/questions/36325212/angular-2-dynamic-tabs-with-user-click-chosen-components/36325468#36325468 for an example – Günter Zöchbauer May 16 '16 at 03:28
  • Günter Zöchbauer, thanks for your help, but your example shows how to create _component from component_. When I would like to find solution for _component from directive_. – rnofenko May 17 '16 at 04:00