4

I know this has been asked so many times (Actually, a lot!), but I still can't manage to fully understand what is the main difference between these two.

I am aware a service returns a singleton instance of the function provided, and that a factory will simply invoke this function and return its value.

But...

As I see it, you can do and achieve the same effects using one or another, so how should I choose which one to use? Why should I choose services over factories, or the other way around?

Is there a situation where one can do something that the other cannot?

6
  • I am aware of the differences stated there, what I am asking however is not the how, but the why. Why should I chose one over the other?. Edited the question's title to evade possible confussions Commented Oct 30, 2014 at 13:42
  • all services, provides and factories are singletons, you may understand the basic of each and then decide what's the best fit for your solution, theres no one size fits all here Commented Oct 30, 2014 at 13:44
  • The problem is that I understand the basic differences, but I cannot see one possible situation when one can do something that the other cannot. With that in mind, I can always stick to one approach and never use the other one. That's not what I intend though. Commented Oct 30, 2014 at 13:46
  • the main difference reside on each object has its own level of configuration prior to send the instance, the answer on the question I referred explain it very clearly Commented Oct 30, 2014 at 13:50
  • Services and factories have the same level of configuration. Providers, however, have a more advanced one. That's why Providers are out of my question, since that's one thing the other approaches don't have or cannot accomplish. Commented Oct 30, 2014 at 13:53

1 Answer 1

3

In Short

If you want your function to be called like a normal function, use factory. If you want your function to be instantiated with the new operator, use service. If you don't know the difference, use factory

Source :

URL : http://iffycan.blogspot.in/2013/05/angular-service-or-factory.html

Detailed Explanation

When you use service , the variables and functions you declare will be prototyped to new object and object will be returned to you. Where in factory , it will return the function simply as it is.

Lets say you created a service with 10 functions , when you include it as dependency, new object will be created and all function will be prototyped , check below example

function Gandalf() { this.color = 'grey'; } Gandalf.prototype.comeBack = function() { this.color = 'white'; } 

"this" will be returned. Performance issues are negligible here.

Sign up to request clarification or add additional context in comments.

2 Comments

There must be something I am missing here, but I still can't understand why would I chose to use service or factory. The factory's function can return an object too: function() { var color = 'grey'; return { comeBack: function() { color = 'white'; } }; }
He chose a service because he is providing it a prototype, a newable object. In TypeScript, CoffeeScript, or ES6 this would be a class. It is an object that instantiated with the new keyword.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.