How to access host component from directive?

前端 未结 6 455
北荒
北荒 2020-12-08 01:56

Say I have the following markup:


Is there any way I can access the component instance

6条回答
  •  时光取名叫无心
    2020-12-08 02:31

    Your directive could be the generic one that can be applied to any of your components. So, in that case, injecting the component in constructor would not be possible, So here is one other way to do the same

    Inject the ViewContainerRef in constructor

    constructor(private _viewContainerRef: ViewContainerRef) { }
    

    and then get it using

    let hostComponent = this._viewContainerRef["_data"].componentView.component;
    

提交回复
热议问题