3 ms·
The benefit of the different types of providers comes in how and when they are used. Constants are especially interesting because they can be injected into the
by michaelw 13y ago
The benefit of the different types of providers comes in how and when they are used.
Constants are especially interesting because they can be injected into the .config function of a module. No other provider can be injected for the very good reason that .config cannot inject transitive dependencies. Because constants cannot have any dependencies (unlike services and factories) and do not change (unlike values) they are safe to inject during the config phase. FYI, the .run function is invoked (and its dependencies injected of course) after all modules have finished loading and all config functions have been run. If your provider needs another provider as part of its init then you can set that up in the .run function.
Remember that DI in angular is not magic. It is quite literally the creation of a closure whereby angular invokes your provider function with your dependencies passed as arguments.
The difference between factories and services has a lot to do with how they will get used and singularity of them.
I think the mistake of the documentation is that it leads the reader down the path of the convenience functions first rather than instilling a deeper understanding of the core DI mechanism.
As always with Angular docs, they are dense and it always pays off to understand what you are reading as deeply as possible. Use the force, read the source.
- guac 13y ago> Constants are especially interesting because they can be injected into the .config function of a module. No other provider can be injected for the very good reason that .config cannot inject transitive dependencies. It should be noted than you can inject providers themselves in .config. Most users do this with built-in $routeProvider (which provides $route). It's not mentioned in the post but he could pass "awesomeProvider" into module.config and (if it was more interesting than just a $get method) do something with it during the config phase. With $routeProvider you would use the .when method to configure angular routing. It's .$get method returns the $route instance.
- randall 13y agoThis makes me think Angular 2.0 hopefully will trim some of the conceptual cruft in a few years. Interesting! Thanks for the insight.
- michaelw 13y ago$routeProvider and $location are special in that can be injected into .config. It may be that they are actually implemented as special constants. I do not believe that non constant providers can be injected. Do you have a fiddle or plunk that shows this working?
- guac 13y agoHow about this: http://jsfiddle.net/pJ8Mt/1/ http://jsfiddle.net/pJ8Mt/1/ It's a poor example since I couldn't think of anything useful off the top of my head but it shows how you might modify a service at config time by creating its provider yourself. Also I believe .config executes immediately so the provider needs to be defined beforehand (in this case above .config, but probably better to define it in another module to ensure it's available at config time). When I defined it after .config angular complained it couldn't find awesomeProvider.
- jwarren 13y agoIs there any chance you could elaborate on some of the terms in your second paragraph?