Firebase loop look up data

筅森魡賤 提交于 2019-12-25 02:25:18

问题


$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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!