Questions tagged [angularjs-factory]

Factory can return anything which can be a class (constructor function), instance of class, string, number or boolean. If you return a constructor function, you can instantiate in your controller.

Factory

  • Gives us the function's return value i.e. you just create an object, add properties to it, then return that same object. When you pass this service into your controller, those properties on the object will now be available in that controller through your factory. (Hypothetical Scenario)

  • Singleton

  • Usage: if you're using an object, you could use the factory provider.

  • Syntax: module.factory('factoryName', function);

AngularJS .factory


module.factory('MyFactory', function() {

    var factory = {}; 

    factory.method1 = function() {
        //...
    }

    factory.method2 = function() {
        //...
    }

    return factory;
});
447 questions
3410
votes
30 answers

AngularJS: Service vs provider vs factory

What are the differences between a Service, Provider and Factory in AngularJS?
301
votes
9 answers

AngularJS : When to use service instead of factory

Please bear with me here. I know there are other answers such as: AngularJS: Service vs provider vs factory However I still can't figure out when you'd use service over factory. From what I can tell factory is commonly used to create "common"…
Justin Khoo
  • 3,261
  • 3
  • 15
  • 13
221
votes
4 answers

AngularJS : Factory and Service?

EDIT Jan 2016: Since this still gets attention. Since asking this I've completed a few AngularJS projects, and for those I mostly used factory, built up an object and returned the object at the end. My statements below are still true, however. EDIT…
Cameron Ball
  • 4,048
  • 6
  • 25
  • 34
106
votes
4 answers

AngularJS : What is a factory?

I've been doing a lot of work on Angular.js and overall I find it to be an interesting and powerful framework. I know there have been a lot of discussions on Services vs. Factories vs. Providers vs. Values, but I am still pretty confused about what…
Code Whisperer
  • 22,959
  • 20
  • 67
  • 85
33
votes
1 answer

how to call service method from ng-change of select in angularjs?

I am new to angular js. I am trying to call factory service method 'getScoreData' from ng-change of select, but not able to get it done. please help. Html code: