Skip to content

Instantly share code, notes, and snippets.

@nutzhub
Last active December 21, 2015 18:48
Show Gist options
  • Save nutzhub/6349662 to your computer and use it in GitHub Desktop.
Save nutzhub/6349662 to your computer and use it in GitHub Desktop.
Show differentiate between service, factory, provider
<!DOCTYPE html>
<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>JS Bin</title>
</head>
<body ng-controller="MyCtrl">
{{serviceOutput}}
<br/><br/>
{{factoryOutput}}
<br/><br/>
{{providerOutput}}
<script>
var app = angular.module( 'app', [] );
var MyFunc = function() {
this.name = "default name";
this.setName = function(name){
this.name = name;
};
this.$get = function() {
return "Hello from MyFunc.$get(). this.name = " + this.name;
};
return "Hello from MyFunc(). this.name = " + 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 );
// Hey We can config provider here!!
app.config(function(myProvProvider){
myProvProvider.setName("new name given");
});
function MyCtrl( $scope, myService, myFactory, myProv ) {
//myService = [object Object]
$scope.serviceOutput = "myService = " + myService;
//myFactory = Hello from MyFunc(). this.name = default name
$scope.factoryOutput = "myFactory = " + myFactory;
//myProvider = Hello from MyFunc.$get(). this.name = new name given
$scope.providerOutput = "myProvider = " + myProv;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment