解除绑定的角元素 [英] unbind an angular element

查看:106
本文介绍了解除绑定的角元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code指令里面,我想确保它得到清理时的范围被破坏。我看了网上以及在code,我想知道我怎么解除绑定的元素。

  VAR窗口= angular.element($窗口);
window.bind(调整大小功能(E){
    美国广播公司();
});

解决方案:

  VAR ABC =功能(){};变种窗口= angular.element($窗口);
window.bind('调整',ABC);范围。在('$破坏',功能(E $){
    window.unbind('调整',ABC);
});


解决方案

从解除绑定窗口时,该指令的范围被摧毁的功能。因此,在你的指令,你会在范围的破坏的事件调用清理功能:

$ $范围在('$毁灭',清理);

创建一个清除函数并调用了jQuery的解除绑定功能

有在<一个示例href=\"http://stackoverflow.com/questions/23031381/how-to-safely-clean-up-angularjs-event-binding-in-a-directive\">this SO进入,但与 关闭 函数,而不是(这似乎类似于解除,但在jqLit​​e不可用)。同时根据该SO条目,您可能需要命名功能,你需要再次引用它作为取消绑定调用的参数。

I have the following code inside of a directive and I want to make sure it gets cleaned up when the scope is destroyed. I've looked online as well as the code and I was wondering how do I unbind an element.

var window = angular.element($window);
window.bind("resize", function(e){
    abc();
});

Solution:

var abc = function() {};

var window = angular.element($window);
window.bind('resize', abc);

scope.$on('$destroy', function(e) {
    window.unbind('resize', abc);
});

Unbind the function from window when the directive's scope gets destroyed. Hence in your directive, you'll invoke a clean up function on the scope's destroy's event:

$scope.$on('$destroy', cleanUp);

Create a cleanUp function and call the jQuery's unbind function.

There is an example in this SO entry, but with the off function instead (which seems similar to unbind, but unavailable in jqLite). Also per this SO entry, you might have to name your function as you'll need to reference it again as a parameter in the unbind call.

这篇关于解除绑定的角元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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