I am creating a counter using angular and this is my first pass. For some reason, when I update the timeRemaining value, the UI doesn't update. I can see that the line which sets the new value is being hit and the new value is indeed different, but it doesn't seem like the $scope value is being updated (or at least the binding is not being triggered). Any ideas what I'm doing wrong?
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);
};
This is my HTML:
<html ng-app="Everyday">
<body>
<div ng-controller="EverydayController">
<div class="time" id="seconds">{{timeRemaining}}</div>
</div>