Skip to content

Angular dependency injection providers

In Angular 1.3, dependency injection is managed through specific provider recipes that define how objects are created and configured. The core provider types include Value, Constant, Factory, and Service.^[angular-01.md]

Value and Constant

Both Value and Constant are used to define simple data objects or primitive values for injection.

  • Constant: Used to define values that should be available during the configuration phase of the application.^[angular-01.md]
  • Value: Used to define values that are generally available at the run phase.^[angular-01.md]

Factory and Service

Factory and Service providers are used to create logic and stateful services, differing primarily in how they are instantiated by the injector^[angular-01.md].

  • Factory: A function or object that returns a value (the service instance). The factory function is invoked to create the instance.^[angular-01.md]
  • Service: A constructor function that is instantiated with the new keyword. It is typically used to define methods and properties via the this keyword.^[angular-01.md]

Data Sharing and Providers

While providers manage the creation of services, the underlying mechanism ensures that components like controllers share the same data source if they rely on the same service instance^[angular-01.md].

Sources

^[angular-01.md]