2016-11-27 91 views
1

我可以使用ref.child('user').on('child_added',function())將用戶內部的內容存儲到數組變量中。Firebase,將快照數據存儲到數組變量

所以這是我的榜樣,

$scope.samplearray = []; 
ref.child('user').on("child_added", function(snapshot) { 
    $scope.samplearray.name = snapshot.val().name; 
    $scope.samplearray.age = snapshot.val().age; 
}); 

然後使用該陣列中使用ng-repeat

+0

難道我的回答幫助? –

回答

1

兒童事件在火力地堡數據庫被異步解僱我的HTML,這是出了問題的,以顯示它盒子在Angular 1.x.在添加到陣列或使用AngularFire後,您將需要直接觸發$digest循環。 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); 
}