Ste*_*rov 7 rest angularjs restangular
是否可以使用Restangular与2个不同的API?我想两者都有setBaseUrl().
Poy*_*maz 31
只需创建两个或更多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');
});
});
Run Code Online (Sandbox Code Playgroud)
而不是全局配置(虽然您仍然可以为共享属性设置全局配置)创建像这样的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()
});
Run Code Online (Sandbox Code Playgroud)