用Angular UI Router解析承诺数组



我试图在击中控制器之前解决一个承诺数组到things:

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

我在这里做错了什么?

我想你需要:

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

因为如果没有外部返回,内部返回就不会发生任何变化

现在resolve.things返回一个承诺,当解析时,将包含一个数组的标题。


加上其他一些调整:

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

最新更新