As you all know we have different strategies for providers: useClass
, useExisting
, useFactory
, useValue
. But what if I would like to add own strategy? Something like:
providers: [ { MyService: MyService, useAsyncFactory: MyAsyncFactory} ]
What is the best way to extend ReflectiveInjector and make Angular use your extended variant? I found the place where it's defined, but still looking for a way to overwrite existing Angular DI mechanism.
P.S.: Please don't ask why I need it and why not to use existing strategies. I'm doing research about Angular DI and the answer will help me to understand it better.
Under the hood Angular doesn't use
ReflectiveInjector
to retrieve component providers so even if you manage to extend ReflectiveInjector it will have no effect on the component providers. You can see it here:The method is called when component requests a dependency, for example
ViewContainerRef
:And this line:
shows that dependency is retrieved from the view node, not a reflective injector. So when you do like this:
you will see that it's not real. It's just a wrapper around
resolveDep
function which encloses over the view and relevant view node.Reflective injector is still used for the host view injectors. This is the injector that you pass when you instantiate a component dynamically:
Here is the relevant code:
And also the module injector is consulted if you the dependency cannot be resolved on the component or the host view injector.
Here is the relevant code: