Skip to content

Instantly share code, notes, and snippets.

@rajivseelam
Created March 26, 2014 08:37
Show Gist options
  • Save rajivseelam/9778939 to your computer and use it in GitHub Desktop.
Save rajivseelam/9778939 to your computer and use it in GitHub Desktop.
Service vs Factory vs Provider
//http://jsfiddle.net/rajivseelam/HS63T/1/
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() {
this.$get = function() {
return {
sayHello: function() {
return "Hello, World!"
}
}
};
});
function MyCtrl($scope, helloWorld, helloWorldFromFactory, helloWorldFromService) {
$scope.hellos = [
helloWorld.sayHello(),
helloWorldFromFactory.sayHello(),
helloWorldFromService.sayHello()];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment