Luddinus
Luddinus

Reputation: 13

Angular - multiple $http "instances"

I would like to create some "repositories", and add a base url to all request but only for that repository.

angular.module('user', [])
    .service('User', function ($http) {
        // SOMETHING LIKE THIS
        $http.setBaseUrl('/api/v1/users');

        this.index = function () {
            // CALL TO api/v1/users/

            return $http('/');
        }

});

I know there is the $httpProvider and I could add there the interceptors, but it will add to ALL REQUESTS and that's not I want.

What can I do?

Upvotes: 0

Views: 95

Answers (1)

vince
vince

Reputation: 1922

You could create a constant.js service file that holds the various base URL strings that you have, and whenever you need to make an $http call, make a call to that specific base url.

Something like...

$http.post(constants.usersURL + "/", data, function(res) {
    ... //returned val
});

The constants.js file would look like this:

angular.module('yourApp')
  .factory('constants', function () {

    var shared = {
      baseURL: "/api/v1/",
      usersURL: "/api/v1/users"
    }

    return shared;

  });

Upvotes: 1

Related Questions