Hello Everyone ,
In this blog we will discuss about the differences b/w services ,provider and factory in angular jS
a)Service-
This will return the actual function with an instance of the function.
Syntax-
module.service( 'serviceName', function );
b)Factory-
This will return the actual value returned by the function.
Syntax-
module.factory( 'factoryName', function );
c)Provider-
This will return the value returned by the $get function of the function.
Syntax-
module.provider( 'providerName', function );
Below is running example depicting Service,Factory and Provider:
<html ng-app="app">
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.1/angular.min.js"></script>
<meta charset=utf-8 />
<title>Example Service,Factory,Provider</title>
</head>
<body ng-controller="MyCtrl">
{{serviceOutput}}
<br/><br/>
{{factoryOutput}}
<br/><br/>
{{providerOutput}}
<script>
var app = angular.module( 'app', [] );
var MyFunc = function() {
this.name = "Ankit";
this.$get = function() {
this.name = "Akki"
return "Hello " + this.name;
};
return "Hello " + this.name;
};
// returns the actual function
app.service( 'myService', MyFunc );
// returns the function's return value
app.factory( 'myFactory', MyFunc );
// returns the output of the function's $get function
app.provider( 'myProv', MyFunc );
function MyCtrl( $scope, myService, myFactory, myProv ) {
$scope.serviceOutput = "Service = " + myService;
$scope.factoryOutput = "Factory = " + myFactory;
$scope.providerOutput = "Provider = " + myProv;
}
</script>
</body>
</html>
Output-
Service = [object Object]
Factory = Hello Ankit
Provider = Hello Akki
0 Comment(s)