Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The event $scope.$on('$destroy') doesn't work updating ionic & angular

I use $interval and need to detect when the controller is destroyed. Until now, I have used the $destroy event and it worked perfectly. For example with this basic code, it prints "destroy" in the console when I go to another page (with a simple <a href="#/myNewUrl"> in myView.html).

angular.module('myModule').controller('myController', ['$scope', function($scope) {

    $scope.$on('$destroy', function() {
        console.log('destroy');
    });

}]);

But since I updated Ionic to the new version (v1.0.0-beta.14), that uses the new version to Angular too (v1.3.6), the $destroy event isn't detect when I go to another page.

Does anybody get the same problem? How can I be resolve it? Thank you for your answer!


EDIT:

I have finally fixed the problem!!! Now, with the new Ionic version, the view is cached automatically. Adding cache-view="false" in the template disable it.

But I found a best way than the destroy event. Ionic added new events (on $ionicView) and now you can detect when you leave the page (and the page stays cached) with: $ionicView.leave.

To get more information: http://ionicframework.com/docs/nightly/api/directive/ionView/

like image 321
Eve-Amandine Avatar asked Dec 29 '14 14:12

Eve-Amandine


1 Answers

Is your template cached? If you don't have cache: false in your state routes, then the controller is not destroyed.

like image 131
Eric G Avatar answered Oct 21 '22 11:10

Eric G