0

Is it possible to access the instance of a service included on a component decorator providers array from out of its scope? The scope is basically all the children components of the component where it's created, but I'd need to access it from a resolver.

I guess there should be some way to get it using the injector, but I'm not sure if the injector has access to the ElementInjector hierarchy.

Pizzicato
  • 1,473
  • 1
  • 16
  • 32
  • What do you mean by out of scope? Any injectable service can be access using ctor injection in any component, if that service is provider of module. – Harshad Vekariya May 06 '20 at 08:33
  • An ElementInjector scope is it's children components, the provider is the component. Check this out: https://angular.io/guide/hierarchical-dependency-injection – Pizzicato May 06 '20 at 08:40
  • I think, this will answer your quesion: https://angular.io/guide/hierarchical-dependency-injection#resolution-rules – Harshad Vekariya May 06 '20 at 08:46
  • Thanks for answering! This doesn't provide the info I need, though. What I'm trying to do is getting that service instance from a resolver, so it's out of its scope. The documentation explains how Angular retrieves a dependency, first using the ElementInjector tree, and then, if not found, in the ModuleInjector tree. But it doesn't say anything about how to access that instance from anywhere out of it's base scope. – Pizzicato May 06 '20 at 09:00
  • If you are Injecting service by means of ElementInjector, you cant access it out of scope and that the point of injecting at element level. What use case let you drive in to this situation? – Harshad Vekariya May 06 '20 at 09:02
  • i might not understanding question properly, sorry for that. but if you wanna use service injected at parent in a child component, you can use @SkipSelf in ctor for that service in child, by that way, it will skip finding service in current component. see this: https://angular.io/guide/hierarchical-dependency-injection#visibility-of-provided-tokens – Harshad Vekariya May 06 '20 at 09:12
  • Here you can read about the issue that originated this situation: https://stackoverflow.com/questions/61610202/services-reset-on-logout-in-angular/61610469 – Pizzicato May 06 '20 at 09:45
  • Thanks!! @SkipSelf is thought to be used on a child of a component that used the ElementInjector, but wants to access other instance of that service. In my case I want to access that instance from a resolver. – Pizzicato May 06 '20 at 09:49

0 Answers0