是否可以使用具有 2 个不同 API 的 Restangular 来工作?
我想拥有setBaseUrl()对彼此而言。
只需创建两个或多个 Restangular 服务并根据需要配置它们,然后注入您想要使用的模块...
UPDATE
这段代码来自 restangular github 页面
// Global configuration
app.config(function(RestangularProvider) {
RestangularProvider.setBaseUrl('http://www.global.com');
RestangularProvider.setRequestSuffix('.json');
});
//First Restangular Service
app.factory('FirstRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://www.first.com');
});
});
//Second Restangular Service
app.factory('SecondRestangular', function(Restangular) {
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer.setBaseUrl('http://www.second.com');
});
});
而不是全局配置(尽管您仍然可以为共享属性设置全局配置)创建像这样的 Restangular 工厂并将它们注入您的控制器...
// Let's use them from a controller
app.controller('MainCtrl', function(Restangular, FirstRestangular, SecondRestangular) {
// GET to http://www.google.com/users.json
// Uses global configuration
Restangular.all('users').getList()
// GET to http://www.first.com/users.json
// Uses First configuration which is based on Global one, therefore .json is added.
FirstRestangular.all('users').getList()
// GET to http://www.second.com/users.json
// Uses Second configuration which is based on Global one, therefore .json is added.
SecondRestangular.all('users').getList()
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)