Angular $provide and $injector
Blogs20152015-08-06
Angular $provide and $injector
While reading the $provide and $injector service, I extract following from angular document for quick retrieving:
To wrap it up, let’s summarize the most important points:
- The injector uses recipes to create two types of objects: services and special purpose objects
- There are five recipe types that define how to create objects: Value, Factory, Service, Provider and Constant.
- Factory and Service are the most commonly used recipes. The only difference between them is that the Service recipe works better for objects of a custom type, while the Factory can produce JavaScript primitives and functions.
- The Provider recipe is the core recipe type and all the other ones are just syntactic sugar on it.
- Provider is the most complex recipe type. You don’t need it unless you are building a reusable piece of code that needs global configuration.
- All special purpose objects except for the Controller are defined via Factory recipes.
| Features / Recipe type | Factory | Service | Value | Constant | Provider |
|---|---|---|---|---|---|
| can have dependencies | yes | yes | no | no | yes |
| uses type friendly injection | no | yes | yes* | yes* | no |
| object available in config phase | no | no | no | yes | yes** |
| can create functions | yes | yes | yes | yes | yes |
| can create primitives | yes | no | yes | yes | yes |
* at the cost of eager initialization by using new operator directly
** the service object is not available during the config phase, but the provider instance is.
