【问题标题】:Firebase loop look up dataFirebase 循环查找数据
【发布时间】:2014-01-12 23:14:48
【问题描述】:
$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);
    });
  });
});

我使用上面的代码查找主数据,然后使用ng-repeat在视图中循环它们

<div ng-repeat="user in authorizedUsers">{{user}}</div>

但循环数据有时只显示。有时意味着我必须刷新几次才能获得数据显示。

有任何想法或有其他更好的方法可以在 firebase 中查找主数据吗?

【问题讨论】:

    标签: angularjs firebase angularjs-ng-repeat


    【解决方案1】:

    回调是异步调用的,因此它们不会发生在 Angular 的摘要范围内。因此,在新的编译发生之前,它们不会应用于页面。

    angularFire 内部使用的解决方案是调用 $timeout(),这将强制 Angular 的编译过程运行。一些实验将揭示完成这项工作的最有效方法。这是蛮力:

    $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);
          });
        });
      });
    });
    

    【讨论】:

    • 啊啊啊啊,我总是忘记这个。谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 2018-03-09
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多