Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在Angular JS中取消来自控制器的$http调用?_Angularjs_Http_Angular Promise - Fatal编程技术网

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

Angularjs 如何在Angular JS中取消来自控制器的$http调用?,angularjs,http,angular-promise,Angularjs,Http,Angular Promise,我是Angular的新手,我正在尝试找出如何从按钮单击取消ajax调用,我已经编写了以下代码,请让我知道如何进一步添加它。 提前感谢 HTML 控制器 paymentSearchApp.controller('paymentSearchCtrl', function (paymentSearchService,paymentSearchResponse, $scope) { paymentSearchResponse.findResponse(requestPayLoadPacked).the

我是Angular的新手,我正在尝试找出如何从按钮单击取消ajax调用,我已经编写了以下代码,请让我知道如何进一步添加它。 提前感谢

HTML

控制器

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");

                        }
                    );

});

在服务中添加其他参数:

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;
}]);
控制器:

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");
  }
);

});
我没有用live code检查这一点,但它应该给出一条指导原则,请参见

在服务中添加其他参数:

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;
}]);
控制器:

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");
  }
);

});
我没有用live code检查这一点,但它应该给出您应该使用的指南,如:

请先阅读此内容。

您应该使用,例如:

先读这篇文章

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();
                        }
                    );