Providers

Value – Factory – Service

http://jsfiddle.net/fo4qpasa/

https://docs.angularjs.org/guide/providers

https://tylermcginnis.com/angularjs-factory-vs-service-vs-provider/

http://stackoverflow.com/questions/15666048/angularjs-service-vs-provider-vs-factory

Value

  • Provide a simple value
var myApp = angular.module('myApp', []);

myApp.value('myValue', 123);

myApp.controller('DemoController', ['myValue', function DemoController(myValue) {
  this.value= myValue;
}]);

Factory

Abilities

  • Ability to use other services (have dependencies)
  • Service initialization
  • Delayed/lazy initialization

What if we want to compute the value?

Just like with the Value recipe, the Factory recipe can create a service of any type, whether it be a primitive, object literal, function, or even an instance of a custom type.

var myApp = angular.module('myApp', []);

myApp.value('a', 123);

myApp.factory('b',['a', function (a){
    return a * 2;
 }]);


myApp.controller('DemoController', ['a','b', function DemoController(a,b) {
  //a = 123
  //b = 246
  this.value = b;
}]);

Services

What if I want to be more OO and have a class called compute


var myApp = angular.module('myApp', []);

myApp.factory('b', function (){
    return new Compute();
 });

myApp.controller('DemoController', ['b', function DemoController(b) {
  //b = 246
  this.value = b.double(123);
}]);

function Compute() {
  this.double= function(a) {
      return a * 2;
  }
}

With services


var myApp = angular.module('myApp', []);

myApp.service('b', Compute);

myApp.controller('DemoController', ['b', function DemoController(b) {
  //b = 246
  this.value = b.double(123);
}]);


function Compute() {
  this.double= function(a) {
      return a * 2;
  }
}


Provider

What if I want to configure the Compute class before injection?

var myApp = angular.module('myApp', []);

myApp.provider('b', function() {
  var factor =  2;

  this.setFactor= function(s) {
     factor = s;
  }

  function Compute() {
     this.double= function(a) {
     	return a * factor;
    }
   }

  this.$get = function() {
    return new Compute();
  };
});

myApp.config(["bProvider", function(bProvider) {
  bProvider.setFactor(3);
}]);

myApp.controller('DemoController', ['b', function DemoController(b) {
  this.value = b.double(123);
}]);

You should use the Provider recipe only when you want to expose an API for application-wide configuration that must be made before the application starts. This is usually interesting only for reusable services whose behavior might need to vary slightly between applications.

Conclusion

To wrap it up, let's summarize the most important points:

  • There are five recipe types that define how to create objects: Value, Factory, Service, Provider and Constant.
  • Factory and Service are the most commonly used recipes. The only difference between them is that the Service recipe works better for objects of a custom type, while the Factory can produce JavaScript primitives and functions.
  • The Provider recipe is the core recipe type and all the other ones are just syntactic sugar on it.
  • Provider is the most complex recipe type. You don't need it unless you are building a reusable piece of code that needs global configuration.

Providers

By Felipe Jaramillo Gómez