仅执行一次的事件处理程序 [英] Event handler that executed only once

查看:58
本文介绍了仅执行一次的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AngularJS中是否有实现 .one()函数所实现的指令或方法用jQuery?

即将事件附加到仅执行一次的元素上.

解决方案

将ng-click连接到所需的元素,然后在控制器的ng-click功能中具有一个变量,您可以将其切换为一个状态,该状态将使ng-click不再执行类似的操作

var firedOnce = false;
scope.myClickFunction = function(){
    if(firedOnce){ return; }
    //other code
    firedOnce = true;
}

您也可以连接一条指令来执行此操作.

编辑:指令方法的Jsfiddle: http://jsfiddle.net/4hDb3/

Is there a directive or a way to implement in AngularJS what .one() function achieves with jQuery?

i.e., to attach an event to an element that gets executed only once.

解决方案

Wire up an ng-click to the element you want, then in the ng-click function in your controller have a variable that you toggle to a state that would make the ng-click no longer execute something like

var firedOnce = false;
scope.myClickFunction = function(){
    if(firedOnce){ return; }
    //other code
    firedOnce = true;
}

You could also wire up a directive to do it too.

edit: Jsfiddle of directive method: http://jsfiddle.net/4hDb3/

这篇关于仅执行一次的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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