jQuery的"$ .when().done()"对于AngularJS [英] jQuery's "$.when().done()" for AngularJS

查看:158
本文介绍了jQuery的"$ .when().done()"对于AngularJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找某种方法来知道何时使用AngularJS完成两个或多个ajax调用,但是我只能使用jQuery来找到它:

I was looking for some way to know when two or more ajax calls have finished using AngularJS but I was only able to find it using jQuery:

$.when(promise3, promise5).done(
    function(threeSquare, fiveSquare) {
        console.info(threeSquare, fiveSquare);
    }
);

和:

var promises = [
    $.getJSON('square/2'),
    $.getJSON('square/3'),
    $.getJSON('square/4'),
    $.getJSON('square/5')
];

$.when.apply($, promises).done(function() {
    console.info(arguments);
});

有什么办法可以使用AngularJS来做类似的事情吗? 感谢您的支持!

is there any way to do something like this using AngularJS? Thanks for your support!

推荐答案

您将需要查看$ q.all();.您可以在此处阅读.

You will want to look at $q.all(); You can read about it here.

这是一个代码段:

所有(承诺)

将多个promise合并为一个promise,当在 所有的输入承诺都得到解决.

Combines multiple promises into a single promise that is resolved when all of the input promises are resolved.

参数promises – {Array.} –一个promises数组. 返回{承诺} –返回将被解决的单个承诺 带有值数组,每个值对应于 promises数组中的相同索引.如果任何诺言是 以拒绝方式解决,这个最终的承诺将得到解决 具有相同的拒绝率.

Parameters promises – {Array.} – An array of promises. Returns {Promise} – Returns a single promise that will be resolved with an array of values, each value corresponding to the promise at the same index in the promises array. If any of the promises is resolved with a rejection, this resulting promise will be resolved with the same rejection.

在1.1.x版中,您应该可以执行以下操作:

In version 1.1.x, you should be able to do something like this:

$q.all([
    $http.get('square/2'),
    $http.get('square/3'),
    $http.get('square/4'),
    $http.get('square/5')
]).then( function(arrayOfResponses) {
    $log.info('all set');
    $log.debug(arrayOfResponses);
});

更新:

从1.1.6版开始,您应该能够执行以下操作:

As of version 1.1.6, you should be able to do the following:

var Square = $resource('square/:id', {id:1});
$q.all([
    Square.get({id:2}).$promise,
    Square.get({id:3}).$promise,
    Square.get({id:4}).$promise,
    Square.get({id:5}).$promise
]).then(function(arrayOfResponses) {
    $log.info('all set');
    $log.debug(arrayOfResponses);
});

这篇关于jQuery的"$ .when().done()"对于AngularJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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