【问题标题】:Angular Promise with $resource带有 $resource 的 Angular Promise
【发布时间】:2016-09-16 17:00:15
【问题描述】:

我有一个角度承诺,它是通过从 $resource 调用自定义 api 生成的,它返回一个 id 数组。我需要在每个 id 上调用另一个 $resource api,一旦加载了所有详细信息,我希望原始调用继续。

MyFactory.loadChildren(id).then(saveSuccess, saveFail);
MyFactory.loadChildren = function(id) {
      return MyResource.children({
        id: id
      }).$promise

        .then(function(response) {
          _.each(response, function(c) {
            MyResource.details({id:c.id}).$promise.then(function(child){
             //do something with the child



            },function(){

            })
          });
          return //all the children details;
        });
    };

我希望在加载所有子详细信息后执行 saveSuccess。我该怎么做?

【问题讨论】:

    标签: angularjs promise angular-promise angular-resource


    【解决方案1】:

    您需要使用 $q.all。所以你的_.each 将填充一个promises 数组,你会调用$q.all(thatArray)。然后 $q.all(thatArray) 中的函数将获得所有这些调用结果的数组,您可以执行 then() 来进行最终调用。 https://www.jonathanfielding.com/combining-promises-angular/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 2016-02-29
      • 2013-02-16
      相关资源
      最近更新 更多