AngularJS - 中止先前的请求

时间:2015-03-20 13:50:09

标签: angularjs angular-promise

如果用户发出新请求,我试图中止之前的请求。我编写了下面的代码,但请求仍在并行运行。有谁知道我做错了什么?

控制器

AppControllers.controller('HomeController',
    ['$scope', 'Services', '$q', function($scope, Services, $q) {

    var canceler = $q.defer();

    $scope.model = {};

    $scope.find = function(id) {
      canceler.resolve();
      canceler = $q.defer();
      Services.find(id, canceler).get().$promise.then(function(result) {
        $scope.model = result;
      });
    };

}]);

服务

AppServices.factory("Services", ['$resource', function($resource) {

  var factory = {};
  var serviceBase = 'https://localhost/';

  factory.find = function(id, canceler) {
    return $resource(serviceBase + 'api/find/:Id', {Id: id}, {
      get: {
        method: 'GET',
        isArray: false,
        timeout: canceler.promise
      }
    });
  };

  return factory;

}]);

0 个答案:

没有答案