Created
March 26, 2014 20:54
-
-
Save luislee818/9793223 to your computer and use it in GitHub Desktop.
Define AngularJS services using provider, factory and service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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