提供范围的破坏$事件的例子吗? [英] Provide an example of scope's $destroy event?

查看:122
本文介绍了提供范围的破坏$事件的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供破坏活动范围的$的例子吗?下面是从<参考文档href=\"http://docs.angularjs.org/api/ng.%24rootScope.Scope#%24destroy\">http://docs.angularjs.org/api/ng.$rootScope.Scope#$destroy


  

$摧毁()


  
  

删除从父当前范围(及其所有的孩子)
  范围。去除意味着到$电话消化()将不再
  传播到目前的范围和它的孩子。拆除也意味着
  当前范围进行垃圾回收。


  
  

该$ destroy()方法通常使用的指令,如ngRepeat为
  管理环的展开


  
  一个范围破坏

就在一个$破坏事件上播出
  这个范围。应用code可以注册一个$破坏事件处理程序
  这将给予机会执行任何必要的清理。



解决方案

演示: http://jsfiddle.net/sunnycpp/ u4vjR / 2 /

在这里,我已经创建的处理灭指令。

  ctrl.directive('handleDestroy',函数(){
    返回功能(范围,tElement,属性){
        (范围。在$('$破坏',功能){
            警报(在毁灭:+ scope.todo.text);
        });
    };
});

Provide an example of scope's $destroy event? Here is 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.

解决方案

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);
        });
    };
});

这篇关于提供范围的破坏$事件的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆