Andrey Yasinishyn
Andrey Yasinishyn

Reputation: 1861

How to configure Angular.js to user default host?

For example, I have this call:

$http({method: 'GET', url: '/someUrl'}).
success(function(data, status, headers, config) {
  // this callback will be called asynchronously
  // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});

I need it to request http://my-api-url.com/someUrl, but I don't want to type it everywhere because it will change in the future.

How can I configure it globally ?

Upvotes: 0

Views: 1759

Answers (2)

camden_kid
camden_kid

Reputation: 12813

Use a constant:

var myApp = angular.module('myApp',[]);

myApp.constant('myUrls', {
    someUrl: 'http://my-api-url.com/someUrl,',
});

mayApp.directive('myDirective', ['myUrls', function (myUrls) {
    return {
        ....
        // use myUrls.someUrl
        ....
    };
}]);

AngularJS Module

Upvotes: 3

Dayan Moreno Leon
Dayan Moreno Leon

Reputation: 5435

i don't think you can, that would cripple any other call to other url so what you could do is setting a host variable in a service pointing to the host you want and then in every call set your url as.

$http.get(service.host+'/somePath'}).
success(function(data, status, headers, config) {

}).
error(function(data, status, headers, config) {

});

Upvotes: 0

Related Questions