0

例如,我有这个电话:

$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.
});

我需要它来请求http://my-api-url.com/someUrl,但我不想在任何地方都输入它,因为它将来会改变。

如何全局配置它?

4

2 回答 2

3

使用常量:

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 模块

于 2014-05-26T11:57:32.087 回答
0

我不认为你可以,这会削弱对其他 url 的任何其他调用,所以你可以做的是在指向你想要的主机的服务中设置一个主机变量,然后在每个调用中将你的 url 设置为。

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

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

});
于 2014-05-26T11:38:53.817 回答