I sometimes wish I had a short and easy to understand explanation of when to use each service type in AngularJS. Here it is:
This service actually uses the "service" keyword in the code, and is the go-to for building helper classes, async handlers, and all-around singletons. Inside of a service you define properties and methods on this, and then you can just call them as public methods on the name of the service object that you inject into your controllers.
You use the factory when you need objects. When creating a factory, you define an object and return it. It's especially useful when you have an array full of object.s
Providers are used when you want to make services and factories that are configurable. This is done with a config method. I rarely ever use these.
The constants are for, well, when you want to use constants defined for your whole module! This could be a string, an array, anything really. The point is that it's an object that can't be set later.
This, like the constant service, is a ingle value or object. While in the constant service uses the constant keyword and is unable to be changed, the value service uses the value keyword and can be changed later.
The posts on this site are written and maintained by Jim Lynch. About Jim...
Want FREE access to
my daily stock tips
"The Triple Gainers"?
Sign up here!