AngularJS : Basic $watch not working
It's because you update the value without Angular knowing.
You should use the $timeout
service instead of setTimeout
, and you won't need to worry about that problem.
function testCtrl($scope, $timeout) { $scope.hello = 0; var t = $timeout( function() { $scope.hello++; console.log($scope.hello); }, 5000); $scope.$watch('hello', function() { console.log('watch!'); });}
Or you could call $scope.$apply(); to force angular to recheck the values and call watches if necessary.
var t = setTimeout( function() { $scope.hello++; console.log($scope.hello); $scope.$apply();}, 5000);