Resolve a promise array with Angular UI Router

两盒软妹~` 提交于 2019-12-10 11:46:16

问题


I'm trying to resolve a promise array to things before hitting my controller:

resolve:{
  things: function($q){
    var promises = [];
    var titles = [];
    var thingRef = ['a1', 'a2', 'a3'];
    angular.forEach(thingRefs, function(thingRef){
      promises.push($firebase(ref.child('things').child(thingRef).child('title')).then(function(title){
        titles.push(title);
      }));
    });

    $q.all(promises).then(function(){
      return titles;
    });
  }
},

What am I doing wrong here?


回答1:


I think you need:

return $q.all(promises).then(function(){
  return titles;
});

Because without that outer return the inner return doesn't go anywhere.

Now resolve.things returns a promise, that when resolved, will contain an Array of titles.


With some other adjustments:

resolve:{
  things: function($q){
    var promises = [];
    var thingRef = ['a1', 'a2', 'a3'];
    angular.forEach(thingRefs, function(thingRef){
      promises.push($firebase(ref.child('things').child(thingRef).child('title')));
    });

    return $q.all(promises);
  }
}


来源:https://stackoverflow.com/questions/23572801/resolve-a-promise-array-with-angular-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!