2014-09-12 68 views
0
if (xhr != null) { // xhr is AJAX call request object. 
    console.log("abort"); 
    canceler.resolve(); 
} 

xhr = $http({ method: 'POST', url:("/Home/GetLibrary"), timeout: canceler.promise }) 
    .success(){ 
     alert("success"); 
    }) 
    .error() { 
     alert("error"); 
    }); 

這裏我檢查xhr對象。如果xhr對象不是null,那麼我放棄了這個調用。但它會中止當前的呼叫,而不是先前的Ajax呼叫。
我嘗試再次調用相同的函數,然後所有後續的AJAX調用都會中止。在AngularJS中取消AJAX請求

回答

1

試試這個

var canceller = $q.defer(); 
      
    $http.get("yourUrl", { data:data }) 
         .then(function(response){ 
            $scope.movie = response.data; 
        }); 
      
    $scope.cancel = function(){ 
        canceller.resolve("user cancelled");  
    }; 

$ scope.cancel();

+0

我試過了。但$ scope.cancel函數不會被調用任何情況。 – chirag 2014-09-12 11:30:55