roonroon roonroon - 5 months ago 19
AngularJS Question

setInterval inside ng-controller won't work without $http

I got this strange problem. For some bizzare reason, my

setInterval
won't work without
$http.get()
inside it. Currently,
$http
is set as a dependency for controller hosting said interval. But if I get rid of them (both dependency and call itself)
setInterval
stops working. I have no idea how to fix this.

Here is code for the controller

main.controller('timeCtrl', function($scope, $http, clockService) {
$scope.time = clockService.timeBase();
setInterval(function() {
$http.get();
$scope.time = clockService.timeBase();
}, 500);
});


After removing the dependancy, it looks like this

main.controller('timeCtrl', function($scope, clockService) {
$scope.time = clockService.timeBase();
setInterval(function() {
$scope.time = clockService.timeBase();
}, 500);
});


But it doesn't work. What the heck is wrong?

Answer

setInterval is JS function and it won't trigger digest cycle so angular won't see that something have changed. try using it's angular version - $interval

P.S. when a promise is resolved it triggers digest cycle so that call to $http did the job, but it's obviously not the way to do what you want

Comments