Search code examples
angularjsrestangular

AngularJS, Restangular - Setting Restangular.baseUrl on run time


I'm using AngularJS and Restangular. How can I define the baseUrl to use on run time? I dont think that I can use run or config methods since is the user the one who selects the server to connect to.

I have this so far:

        var rest = Restangular.withConfig(function(RestangularConfigurer){
            RestangularConfigurer.setBaseUrl(baseUrl);
        });

This works fine when using rest to do the calls BUT when Im in need of using Restangular.copy this Restangular does not have the baseUrl defined.

How could I defined the Restangular.baseUrl for the Global/ Singleton one on run time?


Solution

  • The docs say:

    You can set all these configurations in RestangularProvider or Restangular service to change the global configuration [...]

    This means, you are able to inject Restangular into your controller, directive, service, ... , and call Restangular.setBaseUrl(baseUrl). I can assure that this works with other configuration methods like setDefaultHttpFields too.