我在下面的工厂里有3个资源。资源具有相同的根路径,但仅在控制器和参数的调用方面有所不同。每个资源中仅使用Get。我想知道是否有办法将资源合并为一个并响应三个不同的Get。也许使用以下规范配置$ resource的第3个参数对象(动作),但我不太确定如何。感谢。
{
getFacility: {method:'GET', params: { id: '@id' }, url: api.fsr + 'facility/:id'},
getService: {method:'GET', params:{ id: '@id' }, url: api.fsr + 'service/:id'},
getRole: {method:'GET', params:{ id: '@id' }, url: api.fsr + 'role/:id'}
}

(function () {
'use strict';
var moduleName = 'fsrFactoryModule';
var factoryName = 'fsrFactory';
var requiredModules = ["ngResource", "configModule"];
var dependencyInjection = ['$resource', 'api', internalFunc];
angular.module(moduleName, requiredModules)
.factory(factoryName, dependencyInjection);
function internalFunc($resource, api) {
function facility() {
return $resource(api.fsr + 'facility/:id',{ id: '@id' });
}
function service() {
return $resource(api.fsr + 'Service/:id',{ id: '@id' });
}
function role() {
return $resource(api.fsr + 'FSR/:id', { id: '@id' });
}
return {
//usage: fsrFactory.facility()
facility /*public method*/: facility /*internal method above*/,
//usage: fsrFactory.GetService()
service /*public method*/: service /*internal method above*/,
//usage: fsrFactory.GetRole()
role /*public method*/: role /*internal method above*/,
}
}
})();

答案 0 :(得分:0)
可以创建一个配置对象并使用一个函数来返回所需的资源
var resources ={
facility: 'facility',
service :'Service',
fsr: 'FSR'
}
function myResources(name, id){
return $resource(api.fsr + resources[name] + '/:id',{ id: '@id' }).get({id:id});
}
return {
myResources: myResources
}
然后在控制器中:
$scope.data = fsrFactoryModule.myResources('facility', 123)
答案 1 :(得分:0)
我想使用下面指定的动作参数,但不确定这是否是正确的方法。
{
getFacility: {method:'GET', params: { id: '@id' }, url: api.fsr + 'facility/:id'},
getService: {method:'GET', params:{ id: '@id' }, url: api.fsr + 'service/:id'},
getRole: {method:'GET', params:{ id: '@id' }, url: api.fsr + 'role/:id'}
}