Angularjs Controller destructor

angularjs, angularjs-scope

Solution

You have to listen to the `$destroy` event, e.g.:

function MyController($scope, ...) {
    ...
    $scope.$on("$destroy", function handler() {
        // destruction code here
    });
}

Relevant docs: https://docs.angularjs.org/api/ng/type/$rootScope.Scope

Problem

I have an AngularJs app. I use Controllers for some child scopes. In every Controller I can set a number of variables that belong to the corresponding Child Scope. When AngularJs instantiate a controller, there is a constructor where I can set a default value to my child-scope variables. Do I have a controller "destructor"? How do I know when a controller is closing and the scope is being cleaned (destroyed by the $destroy function)? Thanks!

Original source