angularJS中的自动可更新值

时间:2017-02-07 12:40:19

标签: javascript html angularjs date

我有一系列日期$scope.dates = []$scope.dates[0].date)。我需要创建另一个具有持续时间自动更新(!)值的数组。

  • $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>

编辑:已解决问题

2 个答案:

答案 0 :(得分: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);

    };

答案 1 :(得分:-1)

你应该使用$ interval。 https://docs.angularjs.org/api/ng/service/ $间隔