Edit 04/15 The answers I've gotten so far explain the difference between service/factory/ and provider, but that doesn't really get at what I'm confused about. AngularJS documentation asserts that all three are essentially the same code "under the hood". But if that's the case, why can I only pass a "provider" into a configuration block, as opposed to a "factory"?
Original Question:
According to the documentation for Providers:
The most verbose, but also the most comprehensive one is a Provider recipe. The remaining four recipe types — Value, Factory, Service and Constant — are just syntactic sugar on top of a provider recipe.
That gives me the impression that all services are also providers. But I'm currently dealing with a problem using the $window service in my app's configuration block, and came across this bit of documentation:
Module Loading & Dependencies
A module is a collection of configuration and run blocks which get applied to the application during the bootstrap process. In its simplest form the module consist of a collection of two kinds of blocks:
- Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured.
Which implies there's a distinction between a provider and a service that's more fundamental than a service recipe simply being "syntactic sugar" over the provider recipe. What's the deal?
A provider is a factory that instantiates a service via its $get method. Providers are used to configure a service before the application gets going by being available in a module's config phase.
A service is simply an injected singleton. You can define a service by:
myModule.service('serviceName', ServiceConstructor), where ServiceConstructor is a constructor you would normally use new with.myModule.factory('serviceName', factoryFunction), where factoryFunction is a function that returns the service instance.myModule.value('serviceName', serviceInstance), where serviceInstance is the actual thing that will be injectedmyModule.provider('serviceName', serviceProvider), where serviceProvider is some object with a $get method that returns the service instance.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