As per Angular documentation, we can only inject Providers (not instances) in config blocks. https://docs.angularjs.org/guide/module#module-loading-dependencies
But contrary to this Angular lets you inject $provide
or $inject
in spite of them being singleton service instances.
https://docs.angularjs.org/api/auto/service/$provide
This got me curious so I did some research. Here is what I found:
In code, the reason for 2 is that $provide
is put into the providerCache
before the providerInjector
(the injector used in config blocks) is created. This ensures that it will always be a known provider to the providerInjector
. https://github.com/angular/angular.js/blob/master/src/auto/injector.js#L671
That said, I do agree that being able inject $provide
into config blocks seems to contradict the general rule regarding what can be injected into configuration blocks stated here: https://docs.angularjs.org/guide/module#module-loading-dependencies
Even though it is clearly demonstrated to be something you can do here: https://docs.angularjs.org/guide/module#configuration-blocks
$provide
might just be the one exception to the general rule.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With