Skip to content

Instantly share code, notes, and snippets.

@oeeckhoutte
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oeeckhoutte/9804695 to your computer and use it in GitHub Desktop.
Save oeeckhoutte/9804695 to your computer and use it in GitHub Desktop.
[Angular] Difference between Service, Factory and Provider in AngularJS
var myApp = angular.module('myApp', []);
//service style, probably the simplest one
myApp.service('helloWorldFromService', function() {
this.sayHello = function() {
return "Hello, World!"
};
});
//factory style, more involved but more sophisticated
myApp.factory('helloWorldFromFactory', function() {
return {
sayHello: function() {
return "Hello, World!"
}
};
});
//provider style, full blown, configurable version
myApp.provider('helloWorld', function() {
// In the provider function, you cannot inject any
// service or factory. This can only be done at the
// "$get" method.
var name = 'Default';
return {
$get: function() {
return {
sayHello: function() {
return "Hello, " + name + "!"
}
}
},
setName: function(newName) {
name = newName;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment