我要做的是取消请求或停止侦听特定请求的响应。我不能在请求中使用timeout
。只有在请求发出后才决定是否取消。我已经在ajax jQuery as found in this中看到了解决方案。是否有相同的角度解决方案。我使用$http
来发出POST请求。
答案 0 :(得分:4)
timeout - {number | Promise} - 超时(以毫秒为单位),或承诺在解决时应中止请求。
因此,在发送请求时,将promise传递给配置超时,并在您决定中止时解决它:
var cancelDefer = $q.defer();
$http.get(url, {
timeout: cancelDefer.promise
}).success(...);
// later, to cancel the request:
cancelDefer.resolve("cancelled");
答案 1 :(得分:0)
只是添加我最近使用的 coffeescript 版本:
canceller = $q.defer()
$http.get (requestUrl, { timeout: canceller.promise })
.then (response) ->
$scope.remoteData = response.data;
$scope.cancel = () ->
canceller.resolve 'Cancelled http request'