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

前端 未结 4 798
有刺的猬
有刺的猬 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:58

    You can use a directive to bind specific action on right click, using the contextmenu event :

    app.directive('ngRightClick', function($parse) {
        return function(scope, element, attrs) {
            var fn = $parse(attrs.ngRightClick);
            element.bind('contextmenu', function(event) {
                scope.$apply(function() {
                    event.preventDefault();
                    fn(scope, {$event:event});
                });
            });
        };
    });
    

    Code example on fiddle

提交回复
热议问题