Within my controller, I have a variable named datetime
that gets updated by a timer every second. What I need to accomplish is some work when the day changes. To achieve this, I set up the following watcher:
$scope.$watch("datetime | date: 'MM/dd/yyyy'", function (newDate, oldDate) {
console.log(newDate === oldDate);
}, true);
However, I'm noticing that this listener is initially triggered with newDate
equaling oldDate
. Can anyone shed light on why this might be happening? The code responsible for updating datetime
looks like this:
var timeoutId;
function startTimer() {
timeoutId = $timeout(tick, 1000);
timeoutId.then(startTimer);
}
function stopTimer() {
$timeout.cancel(timeoutId);
timeoutId = undefined;
}
function tick() {
$scope.datetime = new Date();
}
$scope.init = function () {
startTimer();
}
The $scope.init()
function is invoked via ng-init
.