AngularJS:事件处理程序多数民众赞成只执行一次 [英] AngularJS: Event handler thats executed only once

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

问题描述

有一个指令或AngularJS实现什么。一()功能的实现方式jQuery的?

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

也就是说,将事件附加到被执行一次的元素。

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

推荐答案

电线了一个NG-单击您想要的元素,然后在你的控制器中的NG-点击功能有您切换到一个状态将变使NG-点击不再执行类似

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.

修改:指令方式的jsfiddle: http://jsfiddle.net/4hDb3/

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

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

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