Angular: Mehrere Funktionen in einem Service haben
Ist es möglich, mehrere Funktionen in einem Dienst zu haben?
Ich habe dies in meinem Buchservice:
(function() {
'use strict';
angular
.module('app.core')
.service('BookService', BookService);
BookService.$inject = ['$resource'];
/* @ngInject */
function BookService($resource) {
return $resource('/api/book/:id', {
id: '@id'
}, {
'get': {
method: 'GET',
cache: true
},
'query': {
method: 'GET',
isArray: true,
cache: true
},
});
}
})()
Aber im gleichen Dienst möchte ich eine andere Funktion haben, in der ich andere Parameter übergebe, zB:
return $resource('/api/book/:name', {
name: '@name'
}, {
'get': {
method: 'GET',
cache: true
},
'query': {
method: 'GET',
isArray: true,
cache: true
},
});
Meine Steuerung sieht so aus und hat zwei verschiedene Aufrufe:
BookService.get({
id: 2
}, function(book) {})
BookService.get({
name: "bookTitle"
}, function(book) {})