简体   繁体   English

如何在Angular JS中取消待处理的API调用

[英]How to cancel pending API calls in Angular JS

I have a Search box and for every letter a user types I have 4 API calls being made. 我有一个搜索框,对于每个字母,一个用户类型我都会进行4个API调用。 and if user types a long word , many API calls goes in pending state. 如果用户键入一个长字,许多API调用将进入待处理状态。 with every new letter which user types how do i check and cancel if previous call is in pending state . 用户输入的每个新字母,如果先前的通话处于待决状态,我该如何检查和取消。

FController.prototype.typeAheadSearch = function (query) {
    var this_ = this;
    var dfd = this.$q.defer();
    this.$q.all([this.TypeAheadFactory.API.A(query),
                 this.TypeAheadFactory.API.B(query),
                 this.TypeAheadFactory.API.C(query),
                 this.TypeAheadFactory.API.D(query)])
        .then(function (responses) {

    // to do
            dfd.resolve(results);
        });

    return dfd.promise;
}

You can intercept the request and response and work on that. 您可以拦截请求和响应并进行处理。 There is a concept of interceptors in angularjs. angularjs中有一个拦截器的概念。 More can be found here: https://docs.angularjs.org/api/ng/service/ $http 可以在这里找到更多信息: https : //docs.angularjs.org/api/ng/service/ $ http

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM