Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

1 of 6
flup
  • 27.2k
  • 8
  • 56
  • 75

The provider docs have gotten updated since this question was first asked, I suspect, and give a good answer to this question. I've tried to sum it up here.

All Services are singletons, they get instantiated once per app. They can be of any type, whether it be a primitive, object literal, function, or even an instance of a custom type.

The value, factory, service, constant, and provider methods are all providers. They teach the Injector how to instantiate the Services.

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.

  • the Value Recipe is the simplest case, where you instantiate the Service yourself and provide the instantiated value to the injector.
  • The Factory recipe adds the following abilities:
    • ability to use other services (have dependencies)
    • service initialization
    • delayed/lazy initialization
  • The Service recipe produces a service just like the Value or Factory recipes, but it does so by invoking a constructor with the new operator.
  • The Provider recipe is usually overkill. It adds one more layer of indirection by allowing you to configure the creation of the factory.

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

  • The Constant recipe allows you to define services that are available in the config phase. Sooner than services created using the Value recipe. Unlike values, they cannot be decorated using decorator.
flup
  • 27.2k
  • 8
  • 56
  • 75

AltStyle によって変換されたページ (->オリジナル) /