2017-02-07 6 views
0

日付配列$scope.dates = []$scope.dates[0].date)があります。私は、durationのauto-updateble(!)値を持つ別の配列を作成する必要があります。angularJSの自動更新可能な値

  • $scope.dates[i].duration = Date.now() - $scope.dates[i].date

私は秒単位でタイマーを作成したい:

<tr ng-repeat="x in dates"> 
<td>{{x.date | date:'HH:mm:ss'}}</td> 
<td>{{x.duration}}</td> 

編集:Probledはあなたが$間隔を使用する必要があります

答えて

0

この関数を呼び出し、それが$rootScope objectでオブジェクトを維持します:

$rootScope.timerActivate = function() { 

     console.log('activateTimer ::'); 
     if(!$rootScope.time) 
     { 
      $rootScope.time = {} 
     }   
     var countDown = function() { 

      var today = new Date(); 
      var hours = new Date().getHours(); 
      var hours = (hours + 24) % 24; 
      var mid = 'am'; 
      if (hours == 0) { //At 00 hours we need to show 12 am 
       hours = 12; 
      } 
      else if (hours > 12) 
      { 
       hours = hours % 12; 
       mid = 'pm'; 
      } 
      var minute = today.getMinutes(); 
      var sec = today.getSeconds(); 
      $rootScope.time.hours = (hours < 10) ? '0' + hours : hours; 
      $rootScope.time.minutes = (minute < 10) ? '0' + minute : minute; 
      $rootScope.time.seconds = (sec < 10) ? '0' + sec : sec; 
      $rootScope.time.blink = (sec % 2) ? ':' : ' '; 
      $rootScope.time.mid = mid; 
      $timeout(countDown, 1000); 
     }; 

     $timeout(countDown, 1000); 

    }; 
+0

感謝を。問題が解決しました。 –

+1

これを '$ rootScope' makeディレクティブに入れないでください。 – zero298

+0

'today.getHours()'ではなく 'new Date()。getHours()'というのはなぜですか? 'hours =(hours + 24)%24'の予想される効果は何ですか? – RobG

関連する問題