【发布时间】:2017-11-06 17:54:38
【问题描述】:
是否可以在一项服务中拥有多种功能?
我的图书服务中有这个:
(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
},
});
}
})()
但在同一个服务中,我希望有另一个函数,我将传递其他参数,例如:
return $resource('/api/book/:name', {
name: '@name'
}, {
'get': {
method: 'GET',
cache: true
},
'query': {
method: 'GET',
isArray: true,
cache: true
},
});
我的控制器看起来像这样并且有两个不同的调用:
BookService.get({
id: 2
}, function(book) {})
BookService.get({
name: "bookTitle"
}, function(book) {})
【问题讨论】: