【发布时间】:2016-11-25 12:22:52
【问题描述】:
我最近发现了David East's firebase blog post,并开始将我的 $loaded 使用从我的控制器转移到我对每个视图的解析中。出现的一个问题涉及我正在检索带有 firebaseRef 的 $firebaseArray 的情况,其中包含来自另一个 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, currentAuth, ActiveGroup, $cordovaCamera, $ionicScrollDelegate, $ionicModal, $ionicActionSheet, $timeout,$state, moment) {
console.log("what are posts? ",posts); // posts is undefined
提前致谢!
【问题讨论】:
-
您的实际问题不清楚。
-
@theblindprophet 嗯,在我的示例和我引用的文章之间,我的要求很清楚,但我很乐意澄清你是否可以更具体。
-
你问它是否可能?它有效吗?有错误吗?
-
知道了 - 使用上面链接承诺的解析代码,我将“帖子”注入我的控制器并且它返回未定义。当我在第一个 $loaded (for thisGroup) 之后添加控制台日志时,它表明第一个 Promise 工作正常。基本上,我遇到了异步问题,我的控制器没有等待第二个 $loaded 并且“posts”在我的控制器中未定义。
-
请注明
resolve在里面的位置。还提供了如何调用posts。我相信我可以解决这个问题。
标签: angularjs ionic-framework firebase firebase-realtime-database angularfire