11

My application calls $HTTP many times like this:

    this.$http({
        method: this.method,
        url: this.url
    })

The this.url is always set to something like /app/getdata

Now I have moved the back-end of my application to another server and I will need to get data like this:

https://newserver.com/app/getdata

Is there a way that I can supply a base URL that will be used for all the $http calls?

3
  • 1
    Set your baseUrl somewhere in app.js and use baseUrl+this.url wherever required. Something like var baseUrl = "newserver.com/";. Commented Dec 5, 2014 at 11:54
  • 2
    If your back-end is on different server than the angular app, you might also have use Access-Control-Allow-Origin header. See developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS Commented Dec 5, 2014 at 12:22
  • You may see Restangular - there you can change base_url with one method. compassinhand.com/2015/08/05/… Commented Sep 17, 2015 at 9:53

4 Answers 4

21

I found a alternative solution instead of <base> tag: written in coffeescript

$httpProvider.interceptors.push ($q)->
  'request': (config)->
    if config.url.indexOf('/api') is 0
      config.url = BASE_URL+config.url
    config
Sign up to request clarification or add additional context in comments.

3 Comments

This kind of approach is much more appealing to me. I'd prefer interception to lots of injecting constants into my services.
Can someone point me to some documentation to understand how I may use this?
7

I usually keep settings in angular constants and inject them to services.

Comments

1

I tend to keep my urls close to where they are needed. So if I have a service, then I'd keep the base url there; like this: this.rootUrl = '/api/v1/';

This allows me to have additional contextual methods that 'extend' the url.

For example:

this.getBaseUrl = function(client_id, project_id) {
    return this.rootUrl + 'clients/' + client_id + '/projects/' + project_id + '/';
};

Which I can then use like this:

this.createActivity = function(client_id, project_id, activity_name, callback) {
    $http.post(this.getBaseUrl(client_id, project_id) + 'activities', {activity: {name: activity_name}})
        .success(callback)
        .error(this.handlerError);
};

or like this (within the same service):

this.closeActivity = function(activity_id, callback){
    $http.get(this.rootUrl + 'close_activity/' + activity_id)
        .success(callback)
        .error(this.handlerError);
};

Comments

-4

set baseUrl in $rootScope:

app.run(function($rootScope) {
    $rootScope.baseUrl = "https://newserver.com";
});

add $rootScope into your app's controllers:

app.controller('Controller', ['$rootScope', function($rootScope){
    ...

this.$http({
    method: this.method,
    url: $rootScope.baseUrl + this.url
})

1 Comment

This is very poor design. It attaches the shared configuration to an object which exposes hundreds of unrelated concerns. At least use app.value('baseUrl', 'https://newserver.com'); and inject that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.