Mastering Web Application Development with AngularJS

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

Comments

by anonymous   2019-07-21

Explanation

You got different things here:

First:

  • 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

Second:

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

Third:

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

(ref: http://www.amazon.com/Mastering-Web-Application-Development-AngularJS/dp/1782161821).


Usage

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 injectedArg.call(this) 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: http://blog.manishchhabra.com/2013/09/angularjs-service-vs-factory-with-example/


UPDATE :

Excellent post here : http://iffycan.blogspot.com.ar/2013/05/angular-service-or-factory.html

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


UPDATE :

AngularJS team does his work and give an explanation: http://docs.angularjs.org/guide/providers

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 http://docs.angularjs.org/tutorial

you can refer the following sites

  1. http://www.angularjstutorial.com/

  2. http://www.thinkster.io/

3.http://tutorialzine.com/2013/08/learn-angularjs-5-examples/

you can also refer this book

Mastering Web Application Development with AngularJS [Import] [Paperback] you can buy this book from amazon site http://www.amazon.in/Mastering-Web-Application-Development-AngularJS/dp/1782161821

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

http://www.amazon.in/AngularJS-Brad-Green/dp/1449344852

https://www.ng-book.com/

and some pdf for download

http://jan.varwig.org/wp-content/uploads/2013/05/AngularJS-Example.pdf

http://bcs.duapp.com/dreamlu/AngularJS%202013.pdf

http://weblogs.asp.net/dwahlin/archive/2013/07/30/angularjs-in-60-ish-minutes-the-ebook.aspx

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.