Skip to content

Instantly share code, notes, and snippets.

@tastywheat
Last active December 28, 2015 03:39
Show Gist options
  • Save tastywheat/7436143 to your computer and use it in GitHub Desktop.
Save tastywheat/7436143 to your computer and use it in GitHub Desktop.
angular provider
<html ng-app="mymodule">
<head></head>
<body ng-controller="AppController">
</body>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.10/angular.min.js"></script>
<script>
//http://stackoverflow.com/questions/15666048/angular-js-service-vs-provider-vs-factory
angular.module( 'mymodule', [])
//NOTICE: Define provider before config blocks.
.provider('person', function(){
var person = (function(){
function doWork(){
alert('wrking');
}
return {
doWork: doWork
}
})();
this.setName = function(name){
this.name = name;
};
this.$get = function(){
return person;
};
})
//NOTICE: Injected name has 'Provider' appended to 'person' defined above in .provider('person')
.config(function config(personProvider){
personProvider.setName('brian!!');
})
.run(function(person){
person.doWork();
})
.controller( 'AppController', function( $scope ) {
//i dunon, do something
})
;
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment