0

I have an array of resources called companies, and I run some query calls on another resource based on that, and then fire a callback. I tried the following, but the callback gets called straight away:

$q.all(companies.map(function(company) {
  return Person.query({
    companyId: company.id
  });
})).then(function(people) {
  // do stuff
});

http://plnkr.co/edit/OxasetGYU7xwojUNreiA?p=preview

4

1 回答 1

1

您需要提供一个 promise 数组,$q.all让它等待底层的 promise(s) 被实现,使用$promise资源返回的属性。

$q.all(companies.map(function(company) {
  return Person.query({
    companyId: company.id
  }).$promise; //<-- Here
})).then(function(people) {
  console.log(people);
});

演示

于 2014-08-13T23:33:24.633 回答