keepwalking
keepwalking

Reputation: 2654

Angular 2 access component from Injectable

Not really sure how to explain this but i will give my best shot.

I have an injectable provider that is bootstrapped in the main app. The provider its trying to access a component.

I can access that component using ViewChild from other components, but i cannot from the injectable provider. I think its because the provider its initialized before the component, so its not available to the ViewChild query.

Is there a method to use the Viewchild reference from inside the injectable function? Something like initialize the children when i am calling the function.

Sorry if this is confusing, not really sure how to explain.

Thanks, Radu

Upvotes: 3

Views: 1261

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658253

@ViewChild() queries the view. An injectable (service) doesn't have a view, therefore @ViewChild() can't find one. Also Angular doesn't process @ViewChild() annotations in services in the first place, only in directives and components.

What you can do is to query using @ViewChild() and pass the result to a shared service.

Upvotes: 2

Related Questions