我有一个名为的指令menu
,只有当它们不是菜单元素的子元素时,我才需要执行他们的代码。
HTML
<menu>
<menuitem>Test 1</menuitem>
<menuitem>Test 2</menuitem>
<menu> <!-- The directive should not run inside of another menu -->
<menuitem>SubTest 1</menuitem>
<menuitem>SubTest 2</menuitem>
</menu>
</menu>
指示
app.directive('menu', function() {
document.addEventListener('contextmenu', function(){event.preventDefault()}, false);
return {
restrict: 'E',
priority: 800,
link: function(scope, el, attrs) {
var el = el[0];
el.addEventListener("contextmenu", function(){
event.preventDefault();
}, false);
el.parentNode.addEventListener('contextmenu', function(){
menu.style.display = "block";
menu.style.top = event.layerY+"px";
menu.style.left = event.layerX+"px";
}, false);
}
}
});