简体   繁体   English

如何禁用所有ng-click和ng-submit事件

[英]How to disable all ng-click and ng-submit event

is there any way, how can I globally (in service) disable and enable all ng-click and ng-submit events? 有什么办法,我如何全局(在使用中)禁用和启用所有ng-click和ng-submit事件?

For example when user is offline I want to disable all actions till he gets connection back.. 例如,当用户离线时,我想禁用所有操作,直到他恢复连接。

I tried to bind all elements with an onClick event which will call stopImmediatePropagation but it didn't work.. 我试图将所有元素与onClick事件绑定,该事件将调用stopImmediatePropagation,但是没有用。

    $('*[ng-click]').click(function( event ) {
      event.stopImmediatePropagation();
    });

Also this question is a little bit different from this one: Disable ng-click on certain conditions of application for all types of element 这个问题也与此稍有不同: 对所有类型的元素禁用ng-click某些应用条件

I'd like to disable/enable all events in APP globally from service, I'm not able to modify all ng-* calls on all elements in the APP.. 我想从服务中全局禁用/启用APP中的所有事件,但无法修改APP中所有元素上的所有ng- *调用。

Try including a return false too: 也尝试包含return false

$('*[ng-click]').click(function( event ) {
  event.stopImmediatePropagation();
  return false;
});

Snippet 片段

The below snippet demonstrates that multiple event handlers attached to a single <a> works too. 下面的代码片段演示了附加到单个<a>多个事件处理程序也可以工作。

 $(function () { $("a").click(function () { alert("Hello!"); return false; }); $("a").click(function () { alert("Bye!"); return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <a href="#">Click Me</a> 

So finally I end up with temporarily disabling all events on the page using jquery.. I got inspired from this plugin http://ignitersworld.com/lab/eventPause.html which for some reason did not work (without any error) 因此,最后我最终使用jquery暂时禁用了页面上的所有事件 。.我从此插件http://ignitersworld.com/lab/eventPause.html受到启发,由于某种原因,该插件不起作用(没有任何错误)

So I took main parts and put it to this class which is working now using jquery v2.1.1 : 因此,我学习了主要部分并将其放在使用jquery v2.1.1现在可以正常工作的此类中:

var EventManager = function() {
    var self = this;
    var nullFun=function(){};

    var getIndex = function(array,value){
        for(var i=0; i< array.length; i++){
            if(array[i]==value){
                return i;
            }
        }
        return -1;  
    };

    this.pauseEvent = function(elm,eventAry){
        var events = $._data(elm, "events");

        if (events) {
            $.each(events, function(type, definition) {
                if((getIndex(eventAry,type)!=-1)||(eventAry=='')){
                    $.each(definition, function(index, event) {
                        if (event.handler.toString() != nullFun.toString()){
                            if(!$._iwEventPause) $._iwEventPause = {};

                            $._iwEventPause["iw-event" + event.guid] = event.handler;
                            event.handler = nullFun;
                        }
                    })
                }
            })
        }
    };

    this.activeEvent = function(elm,eventAry){
        var events = $._data(elm, "events");
        if (events) {
            $.each(events, function(type, definition) {
                if((getIndex(eventAry,type)!=-1)||(eventAry=='')){
                    $.each(definition, function(index, event) {
                        if (event.handler.toString() == nullFun.toString()){
                            event.handler = $._iwEventPause["iw-event" + event.guid];
                        }
                    })
                }
            })
        }
    };

    this.disableAll = function(el) {
        el = el || $('*');
        el.each(function() {
            self.pauseEvent($(this)[0], '');
        });
        self.pauseEvent($(window)[0], '');
    };

    this.enableAll = function(el) {
        el = el || $('*');
        el.each(function() {
            self.activeEvent($(this)[0], '');
        });
        self.activeEvent($(window)[0], '');
    };

    return this;    
};

var eManager = new EventManager();

eManager.disableAll();
eManager.enableAll();

This will go through window object and all elements on the page, move their event handlers away to _iwEventPause object and replace handlers with dummy function.. When enabling, it will move handlers back so they get normally called.. 这将遍历窗口对象和页面上的所有元素,将其事件处理程序移至_iwEventPause对象,并用虚拟函数替换处理程序。启用后,它将向后移动处理程序,以便通常调用它们。

This solution does not handle event handlers added after disabling.. 此解决方案不处理禁用后添加的事件处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM