In AngularJS, how to detect when user leaves template/page?

I think you have two controllers, one for each template like this:

function Controller_1($scope...){
    ...
}
function Controller_2($scope...){
    ...
}

Well, when you switch from one template to another there’s an event that’s fired called $destroy, you can read up on it here http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy

Let’s say I’m switching from the template with Controller_1 to the template with Controller_2. Controller_1 has an interval I’d like to stop. You can accomplish this with:

function Controller_1($scope, $interval...){
    var myInterval = $interval(...);
    $scope.$on("$destroy", function(){
        $interval.cancel(myInterval);
    });
}

This will mean that when the $scope for Controller_1 is destroyed, the event will be called and the interval will be cleared.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)