Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Angular let you inject the $provide service into config blocks?

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

like image 343
Vikash Kumar Avatar asked Nov 08 '22 21:11

Vikash Kumar


1 Answers

This got me curious so I did some research. Here is what I found:

  1. $injector cannot be injected into config blocks
  2. $provide can be injected into config blocks

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.

like image 149
Benny Johansson Avatar answered Nov 15 '22 10:11

Benny Johansson