问题描述
有人可以提供范围的 $destroy 事件的示例吗?这是来自 http://docs.angularjs.org/api 的参考文档/ng.$rootScope.Scope#$destroy
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()
从父级中移除当前作用域(及其所有子级)范围.删除意味着对 $digest() 的调用将不再传播到当前作用域及其子作用域.移除也意味着当前作用域符合垃圾回收条件.
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.
$destroy() 通常被 ngRepeat 等指令用于管理循环的展开.
The $destroy() is usually used by directives such as ngRepeat for managing the unrolling of the loop.
就在作用域被销毁之前,广播一个 $destroy 事件这个范围.应用程序代码可以注册一个 $destroy 事件处理程序这将给它机会执行任何必要的清理.
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.
推荐答案
Demo: http://jsfiddle.net/sunnycpp/u4vjR/2/
这里我创建了 handle-destroy 指令.
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);
});
};
});
这篇关于有人可以为 AngularJS 中的范围提供 $destroy 事件的示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!