Hi I am new with Angularjs. I am reading about Angular service. I have read the following what-is-difference-between-factory-service-and-provider and service-vs-provider-vs-factory. To me, it seems like they are just different way of achieving same goal (please correct me if I am wrong.) Now I am wondering as they all serve the same purpose which one is idea to use and considered as best practice?
-
Maybe [this blog post](http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/) would shed some light, check it out. – Tomislav Dyulgerov May 13 '15 at 12:22
-
possible duplicate of [Service vs provider vs factory?](http://stackoverflow.com/questions/15666048/service-vs-provider-vs-factory) – scniro May 13 '15 at 12:23
2 Answers
Sounds like the question is when to use which. First it's important to note that they are all providers. Each is a more specialized version of the other starting with provider
. factory
is a specialized version of provider
. value
and service in turn are specialized versions of factory
.
And constant
and value
are specialized versions of each other. Because provider
is at the top can therefore can do what all the other providers do you could get away using just providers. But you would be writing a lot of unnecessary code.
Each specialized provider down the chain, if you could use it would allow you to accomplish the same thing in less code. So you could say the best practice is to use the provider furthest down the chain that accomplishes what you wanted.
Here is a high level image showing you what I mean:
(source: simplygoodcode.com)
And below is the link to the blog post the image is from that gives you additional examples:

- 21,988
- 13
- 81
- 109

- 27,650
- 10
- 79
- 80
There is no best practice, no one is going to tell you that one is better than the other (service vs factory).
Use a provider if you need to configure your service at run time (like you configure your router for example).
To when to use a factory or when to use a service, they are the same.
Factory has the easiest syntax to understand (using reveal module pattern) so that is the most common choice.
On the other hand, services are now more "useful" because they use the same syntax as the Angular 2 services, so if you plan in migrate someday, using services now will help the transition.

- 11,918
- 9
- 64
- 88