我可以使用 ref.child('user').on('child_added',function())
将用户内部的内容存储到数组变量中。
这是我的例子,
$scope.samplearray = [];
ref.child('user').on("child_added", function(snapshot) {
$scope.samplearray.name = snapshot.val().name;
$scope.samplearray.age = snapshot.val().age;
});
然后使用该数组通过 ng-repeat
在我的 html 中显示它
最佳答案
Firebase 数据库中的子事件是异步触发的,这是 Angular 1.x 中开箱即用的问题。您需要在添加到数组后直接触发 $digest 循环或使用 AngularFire。 <强> I recommend you use AngularFire.
普通 SDK 方法
function MyController($scope) {
var ref = firebase.database.ref('items');
var items = [];
ref.on('child_added', function(snap) {
$scope.$evalAsync(function() {
var item = snap.val();
// use a super private property to keep around the key
item.__key = snap.key;
items.push(item);
});
});
}
AngularFire 方法
function MyController($scope, $firebaseArray) {
var ref = firebase.database.ref('items');
// Handles all child events: added, removed, changed, moved
$scope.items = $firebaseArray(ref);
}
关于javascript - Firebase,将快照数据存储到数组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40825872/