Angularjs:供应多$资源URL /数据源的服务? [英] Angularjs: a Service that serves multiple $resource urls / data sources?
问题描述
我有一个角服务/提供商,JSON数据提供给我的控制器,它的伟大工程:
I have an Angular service/provider that serves json data to my controller which works great:
angular.module('myApp.services', ['ngResource']).
factory("statesProvider", function($resource){
return $resource('../data/states.json', {}, {
query: {method: 'GET', params: {}, isArray: false}
});
});
但是我还需要JSON数据从另一个文件起到相同的控制器 counties.json
。
But I also need to serve json data to the same controller from another file counties.json
.
我在哪里可以找到如何我写这两个文件的作用是我的控制器服务?
Where can I find out how to I write a service that serves both files to my controller?
推荐答案
您可以更新的服务回报资源的哈希值,没有一个:
You can update service to return a hash of resources, not a single one:
angular.module('myApp.services', ['ngResource']).
factory("geoProvider", function($resource) {
return {
states: $resource('../data/states.json', {}, {
query: { method: 'GET', params: {}, isArray: false }
}),
countries: $resource('../data/countries.json', {}, {
query: { method: 'GET', params: {}, isArray: false }
})
};
});
您可以使用它加入 .query()
结尾的函数名即 geoProvider.states.query()
和 geoProvider.countries.query()
和 myApp.services
已被注入到控制器,再注入 geoProvider
服务到控制器本身也是如此。
You will be able to use it adding .query()
at the end your function name i.e. geoProvider.states.query()
and geoProvider.countries.query()
and myApp.services
has to be injected into your controller, then inject geoProvider
service into controller itself as well.
这篇关于Angularjs:供应多$资源URL /数据源的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!