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

1

6

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

Aldrin answered 10/6, 2017 at 6:26 Comment(2)
Read up on what a provider is then: docs.angularjs.org/guide/providers (Service, Factory, Provider and more). (Also, all services in angular are singleton instances).Wiatt
@ippi, I think the person who asked the question has gone through the documentations. So, if can NOT answer, please do NOT demotivate people. #StayAwayMurcia
C
1

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.

Chirrup answered 15/7, 2017 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.