我正在使用 angular 创建一个计数器,这是我的第一遍。出于某种原因,当我更新timeRemaining
值时,UI 不会更新。我可以看到设置新值的行被击中并且新值确实不同,但似乎 $scope 值没有被更新(或者至少绑定没有被触发)。任何想法我做错了什么?
var everydayModule = angular.module('Everyday', [])
.factory('animate', function ($window, $rootScope) {
var requestAnimationFrame = $window.requestAnimationFrame ||
$window.mozRequestAnimationFrame ||
$window.msRequestAnimationFrame ||
$window.webkitRequestAnimationFrame;
return function (frame) {
requestAnimationFrame(function () {
$rootScope.$apply(frame);
});
};
});
function countdown(timeRemaining, endDate, animate) {
(function frame() {
var now = new Date();
var oneDay = 24 * 60 * 60 * 1000;
timeRemaining = Math.abs((endDate.getTime() - now.getTime()) / oneDay);
animate(frame);
})();
}
function EverydayController($scope, animate) {
$scope.timeRemaining = 0;
$scope.endDate = new Date(2013, 06, 10);
countdown($scope.timeRemaining, $scope.endDate, animate);
};
这是我的 HTML:
<html ng-app="Everyday">
<body>
<div ng-controller="EverydayController">
<div class="time" id="seconds">{{timeRemaining}}</div>
</div>