如何取消Angular JS中控制器的$ http调用?

时间:2016-10-20 11:59:52

标签: angularjs http angular-promise

我是Angular的新手,我想弄清楚如何从按钮点击取消ajax调用,我写了下面的代码,请让我知道如何进一步添加它。 提前谢谢...... !!!

HTML

<a ng-click="cancelCall()" id="pods-cancel-search-button" title="Cancel" class="form-button primary" ><i class="fa fa-search fa-fw"></i>Cancel Search</a>

服务

paymentSearchApp.service('paymentSearchResponse',['$http', '$log', function($http, $log){
//var response = [];
return {
    findResponse: (function(data){
        return $http({
            url:'data/result.json',
            data:data,
            dataType: 'JSON',
            contentType: 'application/json; characterset=utf-8',
            method: 'POST'
        }).then(function(resp){
            var response = resp;
            return response.data;
        },function(resp){
            $log.error("ERROR occured");
        });
    })
};
 return paymentSearchResponse;


}]);

控制器

paymentSearchApp.controller('paymentSearchCtrl', function (paymentSearchService,paymentSearchResponse, $scope) {

paymentSearchResponse.findResponse(requestPayLoadPacked).then(
                        function(response){
                            //operation on response
                        },function(){
                            alert("error in getting response from payment search service");
                        }
                        $scope.cancel = function(){
                        alert("cancel ajax call");

                        }
                    );

});

2 个答案:

答案 0 :(得分:1)

see How to cancel an $http request in AngularJS?.

add additional parameter in your service:

paymentSearchApp.service('paymentSearchResponse',['$http', '$log', function($http, $log){
  //var response = [];
  return {
    findResponse: (function(data, timeoutCanceler){
      return $http({
        url: 'data/result.json',
        data: data,
        timeout: timeoutCanceler.promise,
        dataType: 'JSON',
        contentType: 'application/json; characterset=utf-8',
        method: 'POST'
      }).then(function(resp){
        var response = resp;
        return response.data;
      },function(resp){
        $log.error("ERROR occured");
      });
    })
  };
  return paymentSearchResponse;
}]);

controller:

paymentSearchApp.controller('paymentSearchCtrl', function (paymentSearchService, paymentSearchResponse, $scope, $q) {

  var timeoutCanceler = $q.defer();
  paymentSearchResponse.findResponse(requestPayLoadPacked, timeoutCanceler).then(
     function(response){
       //operation on response
     },function(){
       alert("error in getting response from payment search service");
     };

  $scope.cancelCall = function(){
    timeoutCanceler.resolve();
    alert("cancel ajax call");
  }
);

});

I did not check this with live code, but it should give a guide lines

答案 1 :(得分:1)

You should use $q, like:

var canceller = $q.defer();

paymentSearchResponse.findResponse(requestPayLoadPacked).then(
                        function(response){
                            //operation on response
                             canceller.resolve(response);
                             return canceller.promise;
                        },function(){
                            alert("error in getting response from payment search service");
                        }
                        $scope.cancel = function(){
                        alert("cancel ajax call");
                        canceller.resolve();
                        }
                    );

Read this manuel First.