问题
$scope.authorizedUsers = [];
fbRef.child('folders/').child($routeParams.folderID).once('value',
function(ss) {
ss.forEach(function(childSnapshot) {
fbRef.child('users/').child(childSnapshot.name()).once('value',
function(user) {
$scope.authorizedUsers.push(user.val());
console.log($scope.authorizedUsers);
});
});
});
I use above code to look up master data then loop them in view using ng-repeat
<div ng-repeat="user in authorizedUsers">{{user}}</div>
but the looped data display sometimes only. Sometime means I have to refresh several time to get the data display.
Any idea or there is other better way to look up master data in firebase?
回答1:
The callbacks are invoked asynchronously, so they don't happen within Angular's digest scope. Thus, they are not going to be applied to the page until a new compile occurs.
The solution that angularFire uses internally is to call $timeout(), which will force Angular's compile process to run. Some experimentation would reveal the most efficient way to get this done. Here's a brute force:
$scope.authorizedUsers = [];
fbRef.child('folders/').child($routeParams.folderID).once('value',
function(ss) {
ss.forEach(function(childSnapshot) {
fbRef.child('users/').child(childSnapshot.name()).once('value',
function(user) {
$timeout(function() {
$scope.authorizedUsers.push(user.val());
console.log($scope.authorizedUsers);
});
});
});
});
来源:https://stackoverflow.com/questions/20765506/firebase-loop-look-up-data