0

Before ng9, I can get the instance by:

Inject the ViewContainerRef in constructor

constructor(private _viewContainerRef: ViewContainerRef) { }

and then get it using

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

But in ng9, there is no _data property in ViewContainerRef:

How can I do in ng9? Thanks!

1
  • Please extend the question with the following. Is this a component or a directive? What are you trying to achieve with the component reference? _data seems to be a private variable on the ViewContainerRef, why do you need to access it? Commented May 22, 2020 at 8:08

1 Answer 1

2

If your directive is applied to a unique kind of component, you can use in constructor

constructor(@Optional() @Host() private component:YourComponent){} 

NOTE: You need "wait" to ngAfterViewInit to get the value of a variable in the component

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.