2017-05-28 52 views
0

如何从$ 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( “值” 的火力地堡

功能(tripPath){...}

回答

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