Google Groups

Re: [angular.js] What is the difference between module.factory and module.service and how might both be applied?


Igor Minar Apr 19, 2012 4:09 PM
Posted in group: AngularJS
factory takes a function which is injected and this function returns the instantiated service instance.

myModule.factory('myService', function(dep1, dep2) {
  //construct the service instance
  return serviceInstance;
});

service will instantiate the service instance using the constructor function provided just as if it was called with the new operator, but the constructor gets injected with dependencies:

myModule.service('myService', function(dep1, dep2) {
  //instantiate "this" just as a constructor would
});

alternatively you might do something like this:

var MyService = function(dep1, dep2) {
}

MyService.prototype.protoMethodA = function() {}
MyService.prototype.protoField = 'some field';

myModule.service('myService', MyService);


So the main difference between factory and service is that factory returns some object, while the service creates new instance and returns "this".

I hope this clears things up.

/i




On Thu, Apr 19, 2012 at 3:40 PM, Nick R <nickre...@gmail.com> wrote:
What if you wanted to inject dependencies?  Is that injecting a dependency into the constructor there?  What if you wanted it to work on minified code?  Can you give a similar example for factories?


On Thursday, April 19, 2012 3:09:49 PM UTC-7, Peter Bacon Darwin wrote:
You use factory if the function you are providing will be called to build the object that you want.  I.E. Angular will do obj = MyFactory() to get the obj.
You use service if the function you are providing is a constructor for the object that you want. I.E. Angular will do obj = new MyFactory() to instantiate the obj. 

Does that make sense?  One of the reasons that the service was introduced was to make it easy to use "classical" OO techniques, such as defining your service as a coffeescript class:

class MyService
  constructor: ($resource)->
    # Some stuff
  someMethod: ()->
etc.

Then module.service('MyService', ['$resource', MyService]) creates the new instance of MyService correctly.

Pete

On 19 April 2012 23:02, ProLoser <dean...@gmail.com> wrote:
On IRC, a few other individuals and I have been a bit confuzzled by how to properly apply these two items in our code.

I believe the main issue is that NO ONE really understands the fundamental difference and purpose.

After reading the docs, I found them to be both circular in definition (EXAMPLE - quickly: having the attribute of being quick) and more importantly, use an almost convoluted degree of industry terminology. Now I don't consider myself an amateur and get most of these fundamental concepts and patterns, but the underlying issue is I have no bloody idea how they are intended to be used and how to apply them.

What I think would help the most is this: Please give a real-world use-case and (more importantly) an analogy to real-world objects so that people trying to learn Angular can understand.

By analogy I mean take the concept of patterns and code completely out of the equation. EXAMPLE:
Abstract class is like the word 'shape'. You can't simply use a shape, you need to specify exactly what kind of shape you are making in order to understand how it will behave. Extending an abstract class is like how a 'square' falls under the classification of 'shape' but you can specify dimension of a square and how it will behave, such as rolling. You CAN however determine general information, such as how many shapes are on the piece of paper, or that every shape can have a color.

Sorry I realize that's long, but I really think this is a very lacking and a lot of people have been asking these very questions.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/iXEGJDcbmHcJ.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To view this discussion on the web visit https://groups.google.com/d/msg/angular/-/6acA8cchPqsJ.

To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.