Context Menu Event (right click)

3,215 views
Skip to first unread message

th...@seddonmedia.co.uk

unread,
Sep 5, 2012, 8:05:40 AM9/5/12
to ang...@googlegroups.com
Have been attempting to create a directive to handle right clicks and disable the default context menu.

All looks good with attaching the event handler except I am struggling to disable the context menu, usually this is achieved by returning false onContextMenu, for example in jQuery: ('#id').bind('contextmenu', function(){ return false; }); (http://jsfiddle.net/thomseddon/TShUC/) suffices,

However doing a similar thing in my angular directive does not achieve the same effect, al la: http://jsfiddle.net/thomseddon/dKugj/
That fiddle shows the expression is being parsed and executed, but the context menu is still popping up?

Also, if anyone thinks this would be useful in core (I do) then I will happily package it up and submit a pull request :-)

Thanks in advance

Andy Joslin

unread,
Sep 5, 2012, 11:57:22 AM9/5/12
to ang...@googlegroups.com, th...@seddonmedia.co.uk
It works if you event.preventDefault(): http://jsfiddle.net/ZL8cP/

th...@seddonmedia.co.uk

unread,
Sep 6, 2012, 4:02:10 AM9/6/12
to ang...@googlegroups.com, th...@seddonmedia.co.uk
Brilliant, thanks Andy

nick....@msn.com

unread,
Nov 26, 2012, 3:29:25 AM11/26/12
to ang...@googlegroups.com, th...@seddonmedia.co.uk
I tried this and got $parse is undefined

I changed the code to :

angular.module('myApp.directives').directive('rightClick', function () {
    return function(scope, element, attr) {
        element.bind('contextmenu', function(event) {
            event.preventDefault();
            var fn = scope.$eval(attr['rightClick']);
            return false;
        });
    }
});

which worked for me.

Any comments?

AngularJs newbie, please be patient !

Olivier Clément

unread,
Jan 15, 2013, 12:52:30 PM1/15/13
to ang...@googlegroups.com, th...@seddonmedia.co.uk
Try this:

angular.module('myApp.directives').directive('rightCLick', ['$parse', function($parse) {
        // ..... Function as in Fiddle ...
    }
]);
Reply all
Reply to author
Forward
0 new messages