angularjs – 当解析返回$firebaseArray需要首先返回$firebaseObject服务?

前端之家收集整理的这篇文章主要介绍了angularjs – 当解析返回$firebaseArray需要首先返回$firebaseObject服务?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近发现了 David East’s firebase blog post并且已经开始将我的$loaded使用量从我的控制器移动到我的每个视图的解析.出现的一个问题涉及我正在使用firebaseRef检索$firebaseArray的情况,该firebaseRef包含来自另一个firebase数据库调用内容.我对第二部分的参考将是这样的:

var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');

这是我只能通过解决方案解开一个承诺的情况,或者我是否接近这样做(ActiveGroup是一个返回用户当前所选组的$firebaSEObject的服务):

.state('tab.chats-main',{
      url: '/chats/main',cache: true,views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chatsTopic.html',controller: 'ChatsTopicCtrl'
        }
      },resolve: {
      currentAuth: authRequire,posts: function($firebaseArray,currentAuth,ActiveGroup){
        var thisGroup = ActiveGroup(currentAuth.uid);
          thisGroup.$loaded().then(function(){
          var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');
          return $firebaseArray(chatRef.limitToLast(100)).$loaded();
        })    
      }
    }
})

然后这是我在控制器中注入帖子的地方:

.controller('ChatsTopicCtrl',function($scope,thisGroup,posts,profile,chatName,chatMessages,ActiveGroup,$cordovaCamera,$ionicScrollDelegate,$ionicModal,$ionicActionSheet,$timeout,$state,moment) {

console.log("what are posts? ",posts); // posts is undefined

提前致谢!

解决方法

所以我发现了一些有效但我不确定这是否是最好的做法.通过单独执行每个firebase调用(并按正确的顺序),这似乎可以解决问题:

.state('tab.chats-main',{
  url: '/chats/main',views: {
    'tab-chats': {
      templateUrl: 'templates/tab-chatsTopic.html',controller: 'ChatsTopicCtrl'
    }
  },resolve: {
    currentAuth: authRequire,thisGroup: function(ActiveGroup,currentAuth){
      return ActiveGroup(currentAuth.uid).$loaded();
    },thisGroup){     
      var chatRef = firebase.database().ref('messages/'+thisGroup.groupId+'/main');
      return $firebaseArray(chatRef.limitToLast(100)).$loaded();    
    }       
}
})

然后是你注入它的控制器:

.controller('ChatsTopicCtrl',moment) {

console.log("what is posts? ",posts); // gives proper result

如果有更好/更推荐的方式,我仍然非常感谢听到它.

猜你在找的Angularjs相关文章