Skip to content

Instantly share code, notes, and snippets.

@oeeckhoutte

oeeckhoutte/gist:9804695

Last active Aug 29, 2015
Embed
What would you like to do?
[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