Mastering Web Application Development with AngularJS

Author: Pawel Kozlowski, Peter Bacon Darwin
This Year Stack Overflow 1
This Month Stack Overflow 2


by anonymous   2019-07-21


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

by anonymous   2019-07-21

Apart from offical tutorial you have mentioned

you can refer the following sites




you can also refer this book

Mastering Web Application Development with AngularJS [Import] [Paperback] you can buy this book from amazon site

and also refer AngularJS from O'REILLY book and you can buy this book from the following link in amazon

and some pdf for download

by anonymous   2018-03-19

From Mastering Web Application Development with Angularjs, by Kozlowski and Bacon, Chapter 3 "Communicating with a Back-end Server", section "The Promise API with $q":

A promise that was resolved or rejected once can't change its state.

There is only one chance of providing promised results. In other words it is not possible to:

  • Resolve a rejected promise
  • Resolve an already resolved promise with a different result
  • Reject a resolved promise
  • Reject a rejected promise with a different rejection reason

Those rules are rather intuitive. For example, it wouldn't make much sense if we could be called back with the information that there are problems with a pizza order delivery after a pizza was successfully delivered (and probably eaten!).

If you provide more code and we can understand better what you are trying to do we might be able to help you.