2015-04-09 01:03:35 +02:00
|
|
|
# .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 !');
|
|
|
|
};
|
|
|
|
|
|
|
|
}]);
|
|
|
|
```
|
|
|
|
]
|