‘There’s no good reason for Angular having services, providers, factories, constants; they all…
Adrian Przybyła
2

Daniel Brain

I’ll apologize for the belated nature of this up front.

That said, there’s one way of doing injection, the difference is in how you define the service that will be injected.

  • There are 6 distinct methods defined on the $provide object, I’ll cover the ones mentioned in your article
  • Service is an abstraction around the factory function which will create a boilerplate factory registration in which it explicitly calls $injector.instantiate function, passing the function you supply.
  • Factory is an abstraction around the provider function which will create a provide internally and create a boilerplate $get function using your supplied function argument as the implementation of the factory.
  • Provider gives you the option to have access to the initialization phase of the service during config, so consuming modules can have option to configure the API your provider exposes, and by way of that, provide configuration to the service which will be instantiated when it’s requested.

It’s basically all niceties to give you access to whichever portion of the pipeline you need without having to go through and repeat code you may not need to write. The same is of course true of value, constant and decorator. They have specific functions and are useful in their given contexts.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.