I have seen both angular.factory() and angular.service() used to declare services; however, I cannot find angular.service
anywhere in official documentation.
What is the difference between the two methods? Which should be used for what (assuming they do different things)?
Here are the primary differences:
Services
Syntax:
module.service( 'serviceName', function );
Result: When declaring serviceName as an injectable argument you will be provided with the instance of a function passed to
module.service
.Usage: Could be useful for sharing utility functions that are useful to invoke by simply appending
( )
to the injected function reference. Could also be run withinjectedArg.call( this )
or similar.Factories
Syntax:
module.factory( 'factoryName', function );
Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to
module.factory
.Usage: Could be useful for returning a 'class' function that can then be new'ed to create instances.
Here is example using services and factory. Read more about AngularJS Service vs Factory.
You can also check the AngularJS documentation and similar question on stackoverflow confused about service vs factory.
app.factory('fn', fn) vs. app.service('fn',fn)
Construction
With factories, Angular will invoke the function to get the result. It is the result that is cached and injected.
With services, Angular will invoke the constructor function by calling new. The constructed function is cached and injected.
Implementation
Factories typically return an object literal because the return value is what's injected into controllers, run blocks, directives, etc
Service functions typically do not return anything. Instead, they perform initialization and expose functions. Functions can also reference 'this' since it was constructed using 'new'.
Conclusion
When it comes to using factories or services they are both very similar. They are injected into a controllers, directives, run block, etc, and used in client code in pretty much the same way. They are also both singletons - meaning the same instance is shared between all places where the service/factory is injected.
So which should you prefer? Either one - they are so similar that the differences are trivial. If you do choose one over the other, just be aware how they are constructed, so that you can implement them properly.
All the answers here seem to be around service and factory, and that's valid since that was what was being asked about. But it's also important to keep in mind that there are several others including
provider()
,value()
, andconstant()
.The key to remember is that each one is a special case of the other. Each special case down the chain allowing you to do the same thing with less code. Each one also having some additional limitation.
To decide when to use which you just see which one allows you to do what you want in less code. Here is an image illustrating just how similar they are:
For a complete step by step breakdown and quick reference of when to use each you can visit the blog post where I got this image from:
http://www.simplygoodcode.com/2015/11/the-difference-between-service-provider-and-factory-in-angularjs/