9

我正在并行调用 4 到 10 个 $http get 调用。但是在用户搜索操作之后,我需要显示到相同的视图但结果不同。我填充了一组数据和新的 $http 查询调用。但是之前的 $http 调用(promises)仍在填充或运行,影响 $scope。

我可能的肮脏解决方案是..

使用多应用程序(多模块)页面,该页面可以具有由动态应用程序控制的动态区域。在用户事件上删除与事件对应的元素。例如,搜索结果,如果我想使用不同的 $http 调用显示来自不同来源的结果。如果用户使用新关键字搜索,我将删除结果 ng-app 元素并引导新元素。

4

1 回答 1

26

你应该使用 1.1.5 版本,它可以取消 $http 请求:

https://github.com/angular/angular.js/blob/master/CHANGELOG.md

// set up a dummy canceler
var canceler = $q.defer();

// use it as a timeout canceler for the request
$http({method: 'GET', url: '/some', timeout: canceler.promise}).success(
    function (data) { alert("this won't be displayed on cancel"); }
).error(
    function (data) { alert("this will be displayed on cancel"); }
)

// now, cancel it (before it may come back with data)
$rootScope.$apply(function() {
    canceler.resolve();
});

您可以对所有请求使用相同的取消器,并在开始新集合之前解决它。

于 2013-06-06T16:09:06.583 回答