0

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?

3
  • Really haven't explained your problem well Commented Jun 12, 2016 at 22:04
  • Suggest reading How to Ask Commented Jun 12, 2016 at 22:34
  • I would like to do something like $resource, where you define a baseUrl on an instance, and all request from that instance will have the baseUrl defined, so I don't have to add the baseUrl to all queries. Commented Jun 13, 2016 at 15:48

1 Answer 1

1

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;

  });
Sign up to request clarification or add additional context in comments.

Comments

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.