我正在嘗試學習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);
});
基於控制檯日誌,定時器工作。但是,網頁值不會更新。
你可以設置一個plunkr? –