Is it possible to use RestAngular.setBaseUrl for two api access points?

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

+6
source share
2 answers

just create two or more Restangular services and configure them the way you want and enter your module that you want to use ...

UPDATE

this code from github relational 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'); }); }); 

instead of global configuration (although you can still set global configuration for common properties), create such Restangular factories and add them to the controller ...

 // Let 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() }); 
+28
source

@ wickY26 has the correct answer, but I wanted to add something important.

If you are going to minimize your code, you need to use the built-in annotation, for example:

 app.factory('FirstRestangular', [ 'Restangular', function(Restangular) { return Restangular.withConfig(function(RestangularConfigurer) { RestangularConfigurer.setBaseUrl('http://www.first.com'); }); }]); 

Pay attention to the square brackets.

0
source

All Articles