What I'm trying to do:
- Multiple different components that make use of a single directive
- when the directive is called, I need to be able to get the parent/host component that the directive is called from.
Plnkr -> http://plnkr.co/edit/Do4qYfDLtarRQQEBphW3?p=preview
In looking at the angular.io documentation, I've found that "Injector" can be used to obtain the parent Component in the constructor
constructor(private el: ElementRef, private hostComponent:Injector){
el.nativeElement.draggable = 'true';
}
In doing so, I get Injector Object. From what I can tell I am then supposed to use
this.hostComponent.get(INJECTORTOKEN)
The issues I'm having difficulty grasping is that, the examples provided in Angular, make the assumption that you know the type of Component to provide in the token. ie:
this.hostComponent.get(Image);
this.hostComponent.get(Box);
In the pnkr example, I have two components in my template
<div>
<h2>Hello {{name}}</h2>
<my-image></my-image> <!-- Uses the "My Directive" -->
<my-box></my-box> <!-- Uses the "My Directive" -->
</div>
My questions is, in the "mydirective.ts". how can I leverage the "injector.get()" when I don't know if the parent is an "my-image" or 'my-box' component.
in the sample provided, the directive is triggered "ondrag()". view the Console, for log messages.
Any assistance is gratefully appreciated.
Thank you kindly.