在AngularJS中,如何在查询更改时停止正在进行的$ http调用

Rag*_*ndy 9 routing http angularjs

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

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

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

rew*_*ten 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();
});
Run Code Online (Sandbox Code Playgroud)

您可以对所有请求使用相同的取消器,并在启动新集之前解决它.