Skip to content

Instantly share code, notes, and snippets.

@luislee818
Created March 26, 2014 20:54
Show Gist options
  • Save luislee818/9793223 to your computer and use it in GitHub Desktop.
Save luislee818/9793223 to your computer and use it in GitHub Desktop.
Define AngularJS services using provider, factory and service
'use strict';
/* Services */
// Demonstrate how to register services
// In this case it is a simple value service.
var servicesModule = angular.module('myApp.services', []).value('version', '0.1');
servicesModule.factory('Foo', function () {
var Klass = function () {};
Klass.prototype.value = 123;
return Klass;
});
servicesModule.factory('Quxx', function (Foo) {
return function () {
console.log("Quxx factory: " + (new Foo()).value);
}
});
var servicesModule2 = angular.module('myApp.services2', ['myApp.services']).value('version', '0.1');
// factory
servicesModule2.factory('barFactory', function (Foo) {
return function () {
console.log("factory: " + (new Foo()).value);
}
});
// provider
servicesModule2.provider('barProvider', function () {
this.$get = function (Foo) {
return function () {
console.log("provider: " + (new Foo()).value);
};
};
});
// service
servicesModule2.service('barService', function (Foo) {
this.run = function () {
console.log("service: " + (new Foo()).value);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment