AngularJS: Event handler thats executed only once

故事扮演 提交于 2019-12-04 01:33:16

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/17449076/angularjs-event-handler-thats-executed-only-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!