user1941747 user1941747 - 1 year ago 155
AngularJS Question

AngularJS : When to use service instead of factory

Please bear with me here. I know there are other answers such as:
Angular.js: service vs provider vs factory?

However I still can't figure out when you'd use service over factory.

From what I can tell factory is commonly used to create "common" functions that can be called by multiple Controllers: AngularJS - Common controller functions

The Angular docs seem to prefer factory over service. They even refer to "service" when they use factory which is even more confusing!

So when would one use service?

Is there something that is only possible or much easier done with service?

Is there anything different that goes on behind the scenes? Performance/memory differences?

Here's an example. Other than the method of declaration, they seem identical and I can't figure out why I'd do one vs the other.

Update: From Thomas' answer it seems to imply that service is for simpler logic and factory for more complex logic with private methods, so I updated the fiddle code below and it seems that both are able to support private functions?

myApp.factory('fooFactory', function() {
var fooVar;
var addHi = function(foo){ fooVar = 'Hi '+foo; }

return {
setFoobar: function(foo){
return fooVar;
myApp.service('fooService', function() {
var fooVar;
var addHi = function(foo){ fooVar = 'Hi '+foo;}

this.setFoobar = function(foo){
this.getFoobar = function(){
return fooVar;

function MyCtrl($scope, fooService, fooFactory) {
//foobars = "Hi fooFactory, Hi fooService"
$scope.foobars = [

Answer Source


You got different things here:


  • If you use a service you will get the instance of a function ("this" keyword).
  • If you use a factory you will get the value that is returned by invoking the function reference (the return statement in factory).

ref: angular.service vs angular.factory


Keep in mind all providers in AngularJS (value, constant, services, factories) are singletons!


Using one or the other (service or factory) is about code style. But, the common way in AngularJS is to use factory.

Why ?

Because "The factory method is the most common way of getting objects into AngularJS dependency injection system. It is very flexible and can contain sophisticated creation logic. Since factories are regular functions, we can also take advantage of a new lexical scope to simulate "private" variables. This is very useful as we can hide implementation details of a given service."



Service : Could be useful for sharing utility functions that are useful to invoke by simply appending () to the injected function reference. Could also be run with or similar.

Factory : Could be useful for returning a ‘class’ function that can then be new`ed to create instances.

So, use a factory when you have complex logic in your service and you don't want expose this complexity.

In other cases if you want to return an instance of a service just use service.

But you'll see with time that you'll use factory in 80% of cases I think.

For more details:


Excellent post here :

"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."


AngularJS team does his work and give an explanation:

And from this page :

"Factory and Service are the most commonly used recipes. The only difference between them is that Service recipe works better for objects of custom type, while Factory can produce JavaScript primitives and functions."

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download