我试图在添加同步数组后从同步数组访问子数组,如下所示:
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,并且我正在尝试在其他用户添加新项目时更新视图。
要响应添加到$firebaseArray()
的新项目,您需要扩展它并覆盖$$added()
,如下所述:https://www.firebase.com/docs/web/libraries/angular/guide/extending-services.html#section-firebasearray
几个相关问题:
- 从Firebase,angularJS检索日期
- 使用 AngularFire 在基于 id 的路径之间连接数据
- 在值/键上连接 AngularFire 路径不起作用(将用户配置文件合并到记录中)