如何在Angular JS中取消挂起的API调用

时间:2015-09-07 14:24:30

标签: angularjs api http angular-http

我有一个搜索框,对于用户输入的每个字母,我都有4个API调用。 如果用户键入一个长字,许多API调用将处于挂起状态。如果之前的呼叫处于待定状态,用户键入的每个新字母如何检查和取消。

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;
}

1 个答案:

答案 0 :(得分:0)

您可以拦截请求和响应并对其进行处理。 angularjs中有一个拦截器的概念。更多信息可以在这里找到: https://docs.angularjs.org/api/ng/service/ $ HTTP

希望这有帮助。

相关问题