[英]In an Angular directive, how do you reapply an event listener after the event has been removed?
This might be a result of me lacking knowledge in Angular and if so, forgive me in advance if that is the case. 这可能是由于我缺乏Angular知识而导致的,如果是这样,请事先谅解。
In Angular, suppose I have a directive which is bound to an element as an attribute. 在Angular中,假设我有一条指令绑定到元素作为属性。
<div my-directive></div>
In the directive, I have a click event which will remove the click event itself. 在指令中,我有一个click事件,它将删除click事件本身。
app.directive("myDirective", function() {
return {
link: function(scope, ele, attr, ctrl) {
ele.on("click", function() {
ele.off("click");
console.log("Click event removed");
});
}
}
});
I want to set the click event on to the same div from this directive again (let's say after a button is clicked). 我想再次将此指令的click事件设置到同一div上(例如,单击按钮后)。 How do I accomplish this? 我该如何完成?
You can use like this- 您可以像这样使用-
app.directive("myDirective", function() {
return {
link: function(scope, ele, attr, ctrl) {
ele.on("click", function() {
ele.unbind("click");
console.log("Click event removed");
angular.element(ele).bind("click");
});
}
}
});
app.directive("myDirective", function() {
return {
link: function(scope, ele, attr, ctrl) {
ele.on("click", function() {
angular.element(ele).off('click');
console.log("Click event removed");
});
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.