Showing posts with label AngularJS Factory. Show all posts
Showing posts with label AngularJS Factory. Show all posts

Tuesday, June 23, 2015

AngularJS Service vs Factory

Form a normal usage perspective both the Service and Factory provide the same functionality, both Service and Factory are Singleton, however there are few differences between the two, let us see on how they differ.

When we invoke a service it returns an instance (object representation) of the function, which is more like initializing the object with new and returning the instance.

When we invoke a factory it returns a class representation of the function, the calling Function/Controller can initiate the response (new). Following is the Syntax for Service and Factory

AngularJS Factory

The Factory implementation in AngularJS is similar to Service, the difference is that factory allows us to define our own objects and return them to the calling controller through the factory methods.

The following example shows how to define and use a factory services in AngularJS