How to remove from scope when not in use in AngularJS?

I checked the controller every few seconds, which in turn causes a quiet call to the backend api. Everything works as expected, except when I go to another section (processed by a separate controller), the checkmark continues to work.

Is it possible to completely remove the controller from the area?

Here's the paste of my current controller:

myApp.controller('SupervisorController', function($scope, supervisord, $timeout) { $scope.supervisord = supervisord; (function tick() { $scope.supervisord.fetch(); $timeout(tick, 2500); })(); }); 
+4
source share
2 answers

In http://docs.angularjs.org/api/ng.$rootScope.Scope it mentions that

Before the scope is destroyed, the $ destroy event broadcasts this scope. The application code can register the $ destroy event handler, which will make it possible to perform any necessary cleaning.

It looks like what you want.

+4
source

A var myInterval = setInterval(tick, 2500); would be to use var myInterval = setInterval(tick, 2500); to run it, and then clearInterval(myInterval); to stop it again (or similar $ timeout.cancel (myInterval) ). To do this, you need access to myInterval in both controllers, so you might consider moving it to the Angular service.

+1
source

All Articles