Provide an example $ destroy event.

Provide an example $ destroy event. Here is the reference documentation from http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy

$ destroy ()

Removes the current region (and all its children) from the parent volume. Removing means that $ digest () calls will no longer extend to the current volume and its children. Removal also implies that the current scope has the right to garbage collection.

$ destroy () is commonly used by directives such as ngRepeat to control loop unrolling.

Before the scope is destroyed, the $ destroy event broadcasts this scope. The application code can register the $ destroy event handler, which will make it possible to perform any necessary cleaning.

+58
angularjs
Jan 19 '13 at 17:29
source share
2 answers

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

Here I created the handle-destroy directive.

ctrl.directive('handleDestroy', function() { return function(scope, tElement, attributes) { scope.$on('$destroy', function() { alert("In destroy of:" + scope.todo.text); }); }; }); 
+94
Jan 19 '13 at 17:30
source share

$destroy can refer to 2 things: a method and an event

1. method is $ scope. $ destroy

 .directive("colorTag", function(){ return { restrict: "A", scope: { value: "=colorTag" }, link: function (scope, element, attrs) { var colors = new App.Colors(); element.css("background-color", stringToColor(scope.value)); element.css("color", contrastColor(scope.value)); // Destroy scope, because it no longer needed. scope.$destroy(); } }; }) 

event - $ scope. $ on ("$ destroy")

See @SunnyShah answer .

+10
Sep 30 '15 at 18:04
source share



All Articles