javascript - 用于 AngularJS 的 jQuery "$.when().done()"

标签 javascript jquery ajax angularjs

我一直在寻找某种方法来知道两个或多个 ajax 调用何时使用 AngularJS 完成,但我只能使用 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 做这样的事情? 感谢您的支持!

最佳答案

你会想看看 $q.all();你可以阅读它here .

这是一个片段:

all (promises)

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

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 中,您应该能够执行如下操作:

$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 开始,您应该能够执行以下操作:

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

关于javascript - 用于 AngularJS 的 jQuery "$.when().done()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17414093/

相关文章:

javascript - 保存并再次显示查看用户选择

javascript - Jquery 对话框中的动态 Div 内容

javascript - 修改 input type=number 使其两边都有箭头

javascript - jquery或javascript是否有类似(flash中的onEnterFrame)的功能

javascript - 使用 JavaScript 解析 .obj 3D 图形文件

javascript - 将跟踪代码升级到 Universal Analytics

javascript - Javascript中的加权随机数生成

javascript - 如何检测 $ 函数背后的库是什么?

javascript - 无法获取从 PHP 到 $.ajax 的 JSON 回显

javascript - jQuery.ajax 只能运行一次