在AngularJS,如何停止对查询变化正在进行$ HTTP调用 [英] In AngularJS, how to stop ongoing $http calls on query change

查看:112
本文介绍了在AngularJS,如何停止对查询变化正在进行$ HTTP调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话是4到10 $ HTTP GET并行调用。但经过用户的搜索行为,我需要展示给同样的看法,但有不同的结果。我设置填充数据和新的$ HTTP查询电话。但是,previous $ HTTP调用(承诺)仍在填充或行动,影响范围$

I'm calling 4 to 10 $http get calls in parallel. But after user search action I need display to same view but with different result. I populate set of data and new $http query calls. But the previous $http invocations(promises) are still populating or in action, effecting $scope.

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

My possible dirty solution is ..

采用多应用(多模块),它可以有一个动态的应用控制的动态区域页面。上的用户事件中删除对应于该事件的元素。例如,搜索结果,如果我想显示来自使用不同的$ HTTP调用不同来源的结果。如果用户新的关键字搜索,我要删除的结果NG-app元素和引导新的元素。

Using multi-app(multimodule) page which can have dynamic regions controlled by a dynamic app. On user events remove the element which correspond to the event. Example, search result, if I want to display results from different sources using different $http calls. If user searches with new keyword , I'm going to remove result ng-app element and bootstrap new element.

推荐答案

您应该使用1.1​​.5版,其中有取消$ HTTP请求的能力:

You should use version 1.1.5, which has the ability to cancel $http requests:

<一个href=\"https://github.com/angular/angular.js/blob/master/CHANGELOG.md\">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();
});

您可以使用相同的消除所有的要求,并解决它只是在开始新集之前。

you can use the same canceler for all the requests, and resolve it just before starting the new set.

这篇关于在AngularJS,如何停止对查询变化正在进行$ HTTP调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆