有人可以为AngularJS中的范围提供$ destroy事件的示例吗? [英] Can someone provide an example of a $destroy event for scopes in AngularJS?
问题描述
有人可以提供范围$ 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.
推荐答案
演示: 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事件的示例吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!