Event handler that executed only once

前端 未结 1 1933
不知归路
不知归路 2021-01-13 14:31

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 on

1条回答
  •  伪装坚强ぢ
    2021-01-13 15:08

    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/

    0 讨论(0)
提交回复
热议问题