formations/javascript/angular/services-2.md

37 lines
562 B
Markdown
Raw Permalink Normal View History

# .cadoles-slide-title[Les services (2/2)]
2015-04-07 22:38:36 +02:00
**Création de services**
.cadoles-xs[
```js
var MyApp = angular.module('MyApp', []);
// Création d'un service via une fonction génératrice (la plus classique)
MyApp.factory('myService', ['$http', function($http) {
var myServiceAPI = {
myServiceMethod: function() {
console.log('Foo !');
}
};
return myServiceAPI;
}]);
// Via un constructeur
MyApp.service('myOtherService', ['$http', function($http) {
this.myServiceMethod = function() {
console.log('Foo !');
};
}]);
```
]