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 に答える