I've setup a controller to tick every few seconds, which in turn makes a restful call to a backend api. Everything is working as expected, except when I navigate to another section (handled by a separate controller), the tick continues to fire.
Is it possible to remove the controller from scope entirely?
Here's a paste of my current controller:
myApp.controller('SupervisorController', function($scope, supervisord, $timeout) {
$scope.supervisord = supervisord;
(function tick() {
$scope.supervisord.fetch();
$timeout(tick, 2500);
})();
});
On http://docs.angularjs.org/api/ng.$rootScope.Scope it mentions that
Just before a scope is destroyed a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it chance to perform any necessary cleanup.
This seems like exactly what you want.
A workaround would be to use var myInterval = setInterval(tick, 2500); to get it going and later clearInterval(myInterval); to stop it again (or the analogous $timeout.cancel(myInterval)). For that you need access to myInterval in both controllers, so you might consider wrapping it in a Angular service.