为什么在AngularJS $事件。preventDefault()出现未prevent上下文菜单? [英] Why does $event.preventDefault() in AngularJS not prevent context menu from appearing?
问题描述
之类的标题说,我不知道为什么 $事件。preventDefault()
不是$ P $出现在右键pventing上下文菜单
Like the title says, I'm not sure why $event.preventDefault()
is not preventing the context menu from appearing on right click.
我已经写这个简单的例子中plunker 来说明问题。
HTML:
<body ng-controller="MainCtrl">
<div id="me" ng-mousedown="stopContext($event)">CLICK ME {{num}}</div>
</body>
使用Javascript:
Javascript:
$scope.stopContext = function(ev) {
$scope.num += 1;
ev.preventDefault();
};
先谢谢了。
推荐答案
在为prevent需要捕获的上下文菜单(和prevent)在的 文本菜单
事件。
In order to prevent the context-menu you need to capture (and prevent) the contextmenu
event.
可惜的是,角没有一个指令,这个事件,所以你必须自己创建一个。
Unfortunately, Angular does not have a directive for this event, so you'll have to create one yourself.
从角源$ C $ C复制(1.2.16版):
"Copying" from the source code of Angular (version 1.2.16):
app.directive('myContextmenu', function ($parse) {
return {
compile: function(tElem, tAttrs) {
var fn = $parse(tAttrs.myContextmenu);
return function(scope, elem, attrs) {
elem.on('contextmenu', function (evt) {
scope.$apply(function () {
fn(scope, {$event: evt});
});
});
};
}
};
});
然后,您可以使用它是这样的:
Then, you can use it like this:
<div id="me" my-contextmenu="stopContext($event)">CLICK ME {{num}}</div>
又见这个 短演示 结果
<子>测试在Chrome,Firefox和Safari浏览器的最新版本,并找到工作。
这篇关于为什么在AngularJS $事件。preventDefault()出现未prevent上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!