是否可以将RestAngular.setBaseUrl用于两个api访问点? [英] Is it possible to use RestAngular.setBaseUrl for two api access points?

查看:127
本文介绍了是否可以将RestAngular.setBaseUrl用于两个api访问点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过2个不同的API使用Restangular? 我想同时使用 setBaseUrl().

Is it possible to work with Restangular with 2 different APIs? I would like to have setBaseUrl() for both.

推荐答案

只需创建两个或多个Restangular服务并根据需要对其进行配置,然后将要使用的模块注入您的模块中即可.

just create two or more Restangular service and config them as you want and inject your module which one you want to use...

更新

此代码来自矩形的github页面

this code from restangular github page

// 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工厂并将它们注入您的控制器...

instead of global config (although you can still set global config for shared properties) create Restangular factories like these and inject them your controller...

// 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()
});

这篇关于是否可以将RestAngular.setBaseUrl用于两个api访问点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆