How do I handle right-click events in angular.js?

前端 未结 4 807
有刺的猬
有刺的猬 2020-11-28 23:23

Is there a way to have an element set up so that it performs one action on left-click (ng-click) and then another action on a right-click?

Right now I have something

4条回答
  •  囚心锁ツ
    2020-11-28 23:46

    One way is using a directive that binds an event handler to contextmenu event. I had hard time stopping bubbling to prevent default menu to show up so added native script handler for document. Tried with e.stopPropagation(), e.preventDefault() , return false etc . Checking for target in document handler seems to work well

    app.directive('rightClick',function(){
        document.oncontextmenu = function (e) {
           if(e.target.hasAttribute('right-click')) {
               return false;
           }
        };
        return function(scope,el,attrs){
            el.bind('contextmenu',function(e){
                alert(attrs.alert);               
            }) ;
        }
    });
    
    
    

    DEMO http://plnkr.co/edit/k0TF49GVdlhMuioSHW7i

提交回复
热议问题