如何使用$ q.all的AngularJS哈希选项?

时间:2022-07-13 00:19:41

I have looked around and found nothing at all to explain how to use this. The documentation states:

我环顾四周,一无所获,解释如何使用它。文件说明:

Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.

返回将使用值的数组/散列解析的单个promise,每个值对应于promises数组/散列中相同索引/键的promise。如果任何承诺通过拒绝得到解决,则此结果承诺将被拒绝并具有相同的拒绝值。

But there is no example.

但没有例子。

Does anyone have any examples of using the key / hash method ?

有没有人有任何使用密钥/哈希方法的例子?

1 个解决方案

#1


12  

There don't seem to be many examples of this, but it should work like this:

似乎没有很多这样的例子,但它应该像这样工作:

// as an object
$q.all({
  one: $http.get('/url1'),
  two: $http.get('/url2')
}).then(function (results) {
  var data1 = results.one;
  var data2 = results.two;
});

// as an array
$q.all([
  $http.get('/url1'),
  $http.get('/url2')
]).then(function (results) {
  var data1 = results[0];
  var data2 = results[1];
});

#1


12  

There don't seem to be many examples of this, but it should work like this:

似乎没有很多这样的例子,但它应该像这样工作:

// as an object
$q.all({
  one: $http.get('/url1'),
  two: $http.get('/url2')
}).then(function (results) {
  var data1 = results.one;
  var data2 = results.two;
});

// as an array
$q.all([
  $http.get('/url1'),
  $http.get('/url2')
]).then(function (results) {
  var data1 = results[0];
  var data2 = results[1];
});