2016-03-03 69 views
0

我正在尝试学习AngularJS。请多多包涵。AngularJS - 网页未更新

目前,我正在尝试创建一个倒数计时器,以更新基于给定时间间隔的网页。

下面是HTML文件:

<!DOCTYPE html> 
<html ng-app="timer_module"> 
<head> 
    <meta charset="utf-8"></meta> 
    <link rel="stylesheet" href="https://bootswatch.com/superhero/bootstrap.min.css"></link> 
</head> 

<body ng-controller="TimerController as controller"> 

    {{"D:" + controller.days + " H:" + controller.hours + " M:" + controller.minutes + " S:" + controller.seconds}} 

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script> 
    <script src="js/timer-module.js"></script> 
</body> 

</html> 

我的JS:

angular.module("timer_module", []) 
.controller("TimerController", function() { 

    var target_date = new Date('Jan, 31, 2017').getTime(); 

    var days = 0; 
    var hours = 0; 
    var minutes = 0; 
    var seconds = 0; 
    var controller = this; 

    setInterval(function() { 
     var currentDate = new Date().getTime(); 
     var secondsLeft = (targetDate - currentDate)/1000; 

     days = parseInt(secondsLeft/86400); 
     secondsLeft = secondsLeft % 86400; 

     hours = parseInt(secondsLeft/3600); 
     secondsLeft = secondsLeft % 3600; 

     minutes = parseInt(secondsLeft/60); 
     seconds = parseInt(secondsLeft % 60); 

     controller.days = days; 
     controller.hours = hours; 
     controller.minutes = minutes; 
     controller.seconds = seconds; 

     console.log(controller.days); 
     console.log(controller.hours); 
     console.log(controller.minutes); 
     console.log(controller.seconds); 
     console.log("---------------------"); 
    }, 1000); 

}); 

基于控制台日志,定时器工作。但是,网页值不会更新。

+0

你可以设置一个plunkr? –

回答

3

功能setInterval不触发digest cycle的角度范围。

相反的setInterval,你可以用角模拟$interval

$interval(function() { 
     // skipped 
    }, 1000); 
2

使用$interval服务,您可以做到这一点。

angular.module("timer_module", []) 
 
    .controller("TimerController", function($interval) { 
 

 
    var target_date = new Date('Jan, 31, 2017').getTime(); 
 

 
    var days = 0; 
 
    var hours = 0; 
 
    var minutes = 0; 
 
    var seconds = 0; 
 
    var controller = this; 
 

 
    $interval(function() { 
 
     var currentDate = new Date().getTime(); 
 
     var secondsLeft = (target_date - currentDate)/1000; 
 

 
     days = parseInt(secondsLeft/86400); 
 
     secondsLeft = secondsLeft % 86400; 
 

 
     hours = parseInt(secondsLeft/3600); 
 
     secondsLeft = secondsLeft % 3600; 
 

 
     minutes = parseInt(secondsLeft/60); 
 
     seconds = parseInt(secondsLeft % 60); 
 

 
     controller.days = days; 
 
     controller.hours = hours; 
 
     controller.minutes = minutes; 
 
     controller.seconds = seconds; 
 

 
     console.log(controller.days); 
 
     console.log(controller.hours); 
 
     console.log(controller.minutes); 
 
     console.log(controller.seconds); 
 
     console.log("---------------------"); 
 
    }, 1000); 
 

 
    });
<!DOCTYPE html> 
 
<html ng-app="timer_module"> 
 

 
<head> 
 
    <meta charset="utf-8"></meta> 
 
    <link rel="stylesheet" href="https://bootswatch.com/superhero/bootstrap.min.css"></link> 
 
</head> 
 

 
<body ng-controller="TimerController as controller"> 
 

 
    {{"D:" + controller.days + " H:" + controller.hours + " M:" + controller.minutes + " S:" + controller.seconds}} 
 

 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js"></script> 
 
</body> 
 

 
</html>