Search code examples
angularjsdestroy

AngularJS $destroy() method don't works


I tryed to destroy a scope in a controller, But after destroying scope it's accessible, Why?

myApp.controller('modalCtrl', ['$scope', function($scope){

    $scope.test = 'ha ha ha';
    console.log($scope.test);  // it laughs
    $scope.$destroy();
    console.log($scope.test);  // it laughs again :/

}]);

Does it related to $digest and timing?


Solution

  • $destroy does two things:

    • Broadcast $destroy on that scope
    • Remove itself from its parent's and siblings' linked lists (no change to its children, they are just left for garbage collection)

    So you can say that it is indeed a timing issue. Your scope will be destroyed but you wont know exactly when it'll be garbage collected. What you do know is that the scope is no longer accessible from parent scopes and for all intents and purposes should be considered 'dead'.