Angular service vs provider vs factory
The difference between angular service, factory and provider when declaring as an injectable argument.
Services
module.service( 'serviceName', function );
When declaring serviceName as an injectable argument you will get a reference to the actual function passed to module.service(…).
Factories
module.factory( 'factoryName', function );
When declaring factoryName as an injectable argument you will get the value that is returned by invoking the function reference passed to module.factory(…).
Providers
module.provider( 'providerName', function );
When declaring providerName as an injectable argument you will get the value that is returned by invoking the $get method of the function reference passed to module.provider(…).
Sample code
HTML:
<div ng-controller="MyCtrl">
<p>{{service}}</p>
<p>{{provider}}</p>
<p>{{factory}}</p>
</div>
Javascript:
var myApp = angular.module('myApp',[]);
var func = function() {
this.name = "func name";
this.$get = function() {
this.name = "func $get name";
return "func.$get(): " + this.name;
}
return "func return value: " + this.name;
}
myApp.service('testService', func);
myApp.provider('testProvider', func);
myApp.factory('testFactory', func);
function MyCtrl($scope, testService, testProvider, testFactory) {
$scope.service = "the Service is actual function value = " + testService;
$scope.provider = "the Provider is actual function value = " + testProvider;
$scope.factory = "the factory is actual function value = " + testFactory;
}
The sample code also can be found on JSFiddle.