簡體   English   中英

為什么我不能取消此Angular HTTP請求?

[英]Why can't I cancel this Angular HTTP request?

我使用的是從找到的代碼 SO后取消我的HTTP請求中端的請求,如果他們超時:

var canceller = $q.defer();

$timeout(function() {
  canceller.resolve();
  alert("HTTP request failed.");
}, 5000);

$http({
  url: endpoint + "/encode",
  timeout: canceller.promise,
  data: {
    post: posts.post[id]
  }
}).success(successFunction);

但是,我一直收到ReferenceError: timeout is not defined控制台中ReferenceError: timeout is not defined 我在這里可能做錯了什么?

因此,如果沒有插件,就很難復制,但是我成功地使用您的代碼取消了http請求。 在這里暴跌

Controller.js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $q, $http, $timeout) {

  $scope.msg = 'Not done it';

  var canceller = $q.defer();

  $scope.doThis = function() {
    $timeout(function() {
      canceller.resolve();
      $scope.msg = "I cancelled it";
    }, 1);

    $scope.msg = "I did it";
    var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
    var request = $http.get( url, {timeout: canceller.promise});
    request.success(function(results) {
      $scope.msg = "I loaded some data";
      $scope.data = results;
    });
  }
});

view.html

 <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>
    <p>{{msg}}</p>
    <a href="" ng-click="doThis()">Do This</a>
    <p>
      {{data}}
    </p>
  </body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM