How can I change the base URL of an AngularJS HTTP call?

后端 未结 4 1970
灰色年华
灰色年华 2021-02-20 10:21

My application calls $HTTP many times like this:

    this.$http({
        method: this.method,
        url: this.url
    })

The this.url is alw

4条回答
  •  礼貌的吻别
    2021-02-20 11:11

    set baseUrl in $rootScope:

    app.run(function($rootScope) {
        $rootScope.baseUrl = "https://newserver.com";
    });
    

    add $rootScope into your app's controllers:

    app.controller('Controller', ['$rootScope', function($rootScope){
        ...
    
    this.$http({
        method: this.method,
        url: $rootScope.baseUrl + this.url
    })
    

提交回复
热议问题