如何从$ firebaseArray范围更新DOM?

时间:2017-05-28 17:00:09

标签: angularjs firebase firebase-realtime-database promise angularfire

如何从$ firebaseArray中更新DOM?
目前,DOM在循环完成之前不会更新。

HTML

  <div>{{trip.load_progress}}% Loading</div>

AngularJS:

   $scope.syncArray = $firebaseArray(ref_read_path);
            $scope.trip = {};
            $scope.trip.load_progress = 1;
            $scope.syncArray.$loaded().then(function(items) {
                    for(var z = 0 ; z < items.length ; z++) {
                        var percentage = Math.round(items.length / 100);
                        if (z == percentage * $scope.trip.load_progress) {
                                $scope.trip.load_progress = $scope.trip.load_progress  + 1;
                        }
                    }
            })  

我使用时也一样:
ref_read_path.once(“值”,Firebase的函数(tripPath){...}

1 个答案:

答案 0 :(得分:1)

通过实施$loaded(),您的代码会在Angular不再意识到的时刻运行。要使Angular获取对范围的更改,请使用$timeout()在摘要周期中运行代码:

$scope.syncArray = $firebaseArray(ref_read_path);
$scope.trip = {};
$scope.trip.load_progress = 1;
$scope.syncArray.$loaded().then(function(items) { 
    $timeout(function() {
        for(var z = 0 ; z < items.length ; z++) {
            var percentage = Math.round(items.length / 100);
            if (z == percentage * $scope.trip.load_progress) {
                    $scope.trip.load_progress = $scope.trip.load_progress  + 1;
            }
        }
    });
})