AngularJS Profiling and Performance Always deregister listeners registered on other scopes other than the current scope


Example

You must always unregister scopes other then your current scope as shown below:

//always deregister these
$rootScope.$on(...);
$scope.$parent.$on(...);

You don't have to deregister listners on current scope as angular would take care of it:

//no need to deregister this
$scope.$on(...);

$rootScope.$on listeners will remain in memory if you navigate to another controller. This will create a memory leak if the controller falls out of scope.

Don't

angular.module('app').controller('badExampleController', badExample);
badExample.$inject = ['$scope', '$rootScope'];

function badExample($scope, $rootScope) {
    $rootScope.$on('post:created', function postCreated(event, data) {});
}

Do

angular.module('app').controller('goodExampleController', goodExample);
goodExample.$inject = ['$scope', '$rootScope'];

function goodExample($scope, $rootScope) {
    var deregister = $rootScope.$on('post:created', function postCreated(event, data) {});

    $scope.$on('$destroy', function destroyScope() {
        deregister();
    });
}