Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a Factory is.

Factory has been defined in other StackOverflow discussions as the following:

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.

I find this explanation to be very difficult to grasp and it doesn't increase my understanding of what a factory is.

Would anyone have any explanations or real life examples to share about what exactly a Factory is and why you should use it in lieu of a Service, Provider, or other?

Update

A service holds a reference to any object.

A factory is a function which returns any object

A provider is a function which returns any function

-phew-

like image 674
Code Whisperer Avatar asked May 16 '13 20:05

Code Whisperer


People also ask

What is factory and service in AngularJS?

factory() is a method that takes a name and function that are injected in the same way as in service. The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not.

What is factory pattern in angular?

Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes. The Factory Method defines a method, which should be used for creating objects instead of using a direct constructor call ( new operator).

What is factory service?

The term service factory, coined in 1989 by Richard B. Chase and Warren J. Erikson, represents the idea that the factory can be a source of customer service in addition to a place where products are manufactured.

What is factory method in AngularJS Mcq?

AngularJS Factory Method makes the development process of AngularJS application more robust. A factory is a simple function which allows us to add some logic to a created object and return the created object.


1 Answers

From what I understand they are all pretty much the same. The major differences are their complexities. Providers are configurable at runtime, factories are a little more robust, and services are the simplest form.

Check out this question AngularJS: Service vs provider vs factory

Also this gist may be helpful in understanding the subtle differences.

Source: https://groups.google.com/forum/#!topic/angular/hVrkvaHGOfc

jsFiddle: http://jsfiddle.net/pkozlowski_opensource/PxdSP/14/

author: Pawel Kozlowski

var myApp = angular.module('myApp', []);  //service style, probably the simplest one myApp.service('helloWorldFromService', function() {     this.sayHello = function() {         return "Hello, World!";     }; });  //factory style, more involved but more sophisticated myApp.factory('helloWorldFromFactory', function() {     return {         sayHello: function() {             return "Hello, World!";         }     }; });  //provider style, full blown, configurable version      myApp.provider('helloWorld', function() {     // In the provider function, you cannot inject any     // service or factory. This can only be done at the     // "$get" method.      this.name = 'Default';      this.$get = function() {         var name = this.name;         return {             sayHello: function() {                 return "Hello, " + name + "!";             }         };     };      this.setName = function(name) {         this.name = name;     }; });  //hey, we can configure a provider!             myApp.config(function(helloWorldProvider){     helloWorldProvider.setName('World'); });   function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {      $scope.hellos = [         helloWorld.sayHello(),         helloWorldFromFactory.sayHello(),         helloWorldFromService.sayHello()]; }​ 
like image 159
Jonathan Palumbo Avatar answered Sep 19 '22 15:09

Jonathan Palumbo