If you do not unregister the event, you will receive a memory leak, since the function that you pass to $on will not be cleared (since the link to it still exists). More importantly, any variables that reference links in their area will also be skipped. This will cause your function to be called several times if your controller is created / destroyed several times in the application. Fortunately, AngularJS provides several useful methods to avoid memory leaks and unwanted behavior:
The $on method returns a function that can be called to uninstall the event listener. You want to save your de-registration function as a variable for later use: var cleanUpFunc = $scope.$on('yourevent', ...); See Documentation for $on : http://docs.angularjs.org/api/ng.$rootScope.Scope#$on
Whenever the cleanup area is cleared in Angular (i.e. the controller is destroyed), the $destroy event is fired into that area. You can register for the $scope $destroy event and call cleanUpFunc from this.
You can link these two useful things to choose the right subscription. I gave an example of this: http://plnkr.co/edit/HGK9W0VJGip6fhYQQBCg?p=preview . If you comment the line cleanUpFunc(); and then press the switch button several times and do something, you will notice that our event handler is called several times, which is really not required.
Now, after all, so that your specific situation behaves correctly, just change your code in QuestionsStatusController2 to the following:
angular.module('test') .controller('QuestionsStatusController2', ['$rootScope', '$scope', '$resource', '$state', function ($rootScope, $scope, $resource, $state) { var cleanUpFunc = $rootScope.$on('action2@QuestionStatusController1', function {
cleanUpFunc() calling cleanUpFunc() in $destroy , your event listener for the action2@QuestionStatusController1 event will not be signed and you will no longer skip memory when your controller is cleared.
Polaris878 Sep 17 '13 at 17:55 2013-09-17 17:55
source share