2016-02-22 30 views
0

我試圖從syncronized陣列訪問一個孩子訪問補時後,孩子已添加像這樣:火力地堡 - 從同步陣列返回-1

var postsRef = new Firebase("https://bragr.firebaseio.com/posts"); 
var ref = new Firebase("https://bragr.firebaseio.com"); 
var auth = $firebaseAuth(ref); 
$scope.array.$loaded().then(function() { 
    // NEW POST IS ADDED: 
    postsRef.on('child_added', function(childSnapshot, prevChildKey) { 
     var index = $scope.posts.$indexFor(id); 
     var tmpPost = $scope.posts[index]; 
     console.log(tmpPost); // LOGS -1 
    }); 
}); 

看來,當添加新郵,索引返回-1,這意味着該項目還不在列表中。我認爲child_added函數在孩子被添加到列表中時被調用。但似乎我錯了。

如何確保項目在添加後可以從列表中訪問? 我正在使用Angular Fire,當另一個用戶添加一個新項目時,我試圖更新視圖。

回答