0

How do I get the root dependency injector in angular2? I cant inject the injector in the class constructor(lets assume its a constraint). So in the ideal case I would like to do something like this:

doStuffMethod(){
   let di = getRootDI();
   di.get(NeededType);
}

I know I can either inject the di into the constructor or configure a child dependency injector using ReflectiveInjector. But the problem is that I cant change the constructor signature and I have the right dependency already configured for the root injector.

**Edit: My question is not about putting stuff into a well know object aka ServiceLocator and resolve from there. The question thats suggested as a duplicate to mine is about how to get the reference to DI when its not yet there. After its there you put it into a service locator and you are done. I need to get the reference to root DI using angular and I need it when app is initialized.

Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59
  • Possible duplicate of [Storing injector instance for use in components](http://stackoverflow.com/questions/39409328/storing-injector-instance-for-use-in-components) – Estus Flask Oct 21 '16 at 13:23
  • Its not as I dont need a reference to root injector before its initialized. I need it after its initialized. Also putting the injector in a service locator is something I would like to avoid. – Yaroslav Yakovlev Oct 21 '16 at 13:33
  • Explaining what exactly is your case and why you can't 'change the constructor signature' would help. But it is a duplicate in its current state. You can get an injector via DI if you need it in injectable class. You need to store the reference to root injector first and get it later if you need it in non-injectable class. There's no magical `getRootDI`. – Estus Flask Oct 21 '16 at 13:38
  • @estus read my comment of why my question is different. The question is about how to do it without the constructor DI. If its easier for anyone - make it an academic constraint. Knowing how to do things in multiple ways is something that will not hurt. Really appreciate your comments, just want to kind of extend my knowledge of angualr and know how to do things differently. – Yaroslav Yakovlev Oct 21 '16 at 13:41
  • The comment doesn't explain what your class is and why it cannot be injectable, thus this is possibly an XY problem. Consider `ServiceLocator.injector` a constant you're talking about. You can change internally defined `ServiceLocator` object to `window` global, the pattern stays the same. There are no 'multiple ways' out of this situation afaik. – Estus Flask Oct 21 '16 at 13:49
  • @estus afaik you have child injectors in ng2 components. In case child injector doesnt find a dependency it asks for the parent injector till it reaches the root one. So thats something Id like to have in the service. Does it make the question more clear this way? If not - I can give the reasons why I cant inject anything to the constructor etc(like 3rd party code which I cant touch at some places). But technically the questions remains the same as I have described it. – Yaroslav Yakovlev Oct 21 '16 at 14:04
  • Sorry, it's not really clearer this way. Some code and MCVE would help. If it's third-party code, you have to extend or wrap the class with injectable class, it's usually as simple as that. Normally you can't get root injector instance at all in A2. And normally there's no need for that. If child injector instance is not enough, this means that providers were organized the wrong way. – Estus Flask Oct 21 '16 at 14:14
  • @estus, I really need the answer to my question. I got it that you think I dont need it, still I need it. Now with a lot of comments from you its a lower chance anyone will really answer it. What options do I have to get the answer now? Can you please provide the link to sources/docs that are proving one cant get the reference to root di? I dont mind to know I cant, but I need to be sure its like that. So please, if you have an answer or a link to the code - feel free to share it. – Yaroslav Yakovlev Oct 22 '16 at 23:08
  • A2 wasn't engineered to expose the injector (there can hardly exist a proof of something that doesn't exist), so creating a reference to it on bootstrap is reasonable solution. I'm not sure why you are uncomfortable with the answer that was marked as duplicate, but it's the only good solution here to my knowledge. I'm not implying that you don't need it, but there is a high possibility that it is an XY problem that may have a better alternative solution. It is up to you if you want to have it or not. – Estus Flask Oct 23 '16 at 01:04
  • I don't see how a number of comments may ruin your chances. If the expert knows the answer to your question, he/she will answer. There are not so many good A2 experts on SO, and the chances to get an answer to non-RTFM question are pretty low anyway. – Estus Flask Oct 23 '16 at 01:08

0 Answers0