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

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

问题描述

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

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

解决方案

将 ng-click 连接到您想要的元素,然后在控制器的 ng-click 函数中有一个变量,您可以将其切换到一个状态使 ng-click 不再执行类似

var fireOnce = false;scope.myClickFunction = function(){if(firedOnce){ 返回;}//其他代码发射一次 = 真;}

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

编辑:指令方法的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天全站免登陆