Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone provide an example of a $destroy event for scopes in AngularJS?

Tags:

angularjs

Can someone please provide an example of scope's $destroy event? Here is the reference documentation from http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy

$destroy()

Removes the current scope (and all of its children) from the parent scope. Removal implies that calls to $digest() will no longer propagate to the current scope and its children. Removal also implies that the current scope is eligible for garbage collection.

The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.

Just before a scope is destroyed a $destroy event is broadcasted on this scope. Application code can register a $destroy event handler that will give it chance to perform any necessary cleanup.

like image 717
SunnyShah Avatar asked Jan 19 '13 17:01

SunnyShah


People also ask

What is scope in AngularJS with example?

AngularJS Scope The scope is the binding part between the HTML (view) and the JavaScript (controller). The scope is an object with the available properties and methods. The scope is available for both the view and the controller.

What is $destroy in AngularJS?

$scope.$destroy() This means that calling $scope. $destroy() manually from example within a directive's link function will not remove a handler attached via for example element. on , nor the DOM element itself.

Does AngularJS support scope inheritance?

The $scope object used by views in AngularJS are organized into a hierarchy. There is a root scope, and the $rootScope can has one or more child scopes.


1 Answers

Demo: http://jsfiddle.net/sunnycpp/u4vjR/2/

Here I have created handle-destroy directive.

ctrl.directive('handleDestroy', function() {     return function(scope, tElement, attributes) {                 scope.$on('$destroy', function() {             alert("In destroy of:" + scope.todo.text);         });     }; }); 
like image 137
SunnyShah Avatar answered Sep 24 '22 02:09

SunnyShah