What is the difference between Angular Service and Factory?

Posted in Articles

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

Many people new to Angular will wonder what is the difference between an Angular Service and an Angular Factory, and when to use which.  They look the same, they seem to work the same.  What’s the difference?

From the Angular docs, we know that they are both Angular Providers (as are Angular Value and Angular Constant).

The screen shot from the Angular doc boils it down …

difference between angular service and factory

difference between angular service and factory

If you find yourself creating a factory just to “new” some object, then use service because it has a simpler syntax and it does the ‘new’ for you.  Otherwise, just use factory.  Service is just ‘syntactic sugar’ on factory.

Behind the scenes, service makes a call to factory, which in turn makes a call to provider.

For example of a factory and example of a service, take a look at our “Learn Angular by Example” tutorials.