1 回答
TA贡献1810条经验 获得超5个赞
我使用的trick是,利用冒泡达到点击菜单外部关闭菜单的目的:
var demo = angular.module('demo', []);
demo.directive('menu', function(){
return {
restrict: 'E',
scope: {},
link: function(scope, element, attrs) {
scope.displayMenu = false;
scope.showMenu = function(e) {
scope.displayMenu = true;
e.stopPropagation();
};
scope.clickInner = function(e) {
e.stopPropagation();
};
document.addEventListener('click', function(){
scope.displayMenu = false;
scope.$apply();
}, false);
},
template: '<button class="showBtn" ng-click="showMenu($event)">显示菜单</button>' +
'<div class="menu" ng-class="{show: displayMenu}" ng-click="clickInner($event)">' +
'<ul>' +
'<li>菜单一</li>' +
'<li>菜单二</li>' +
'<li>菜单三</li>' +
'</ul>' +
'</div>'
};
});
在线例子: jsfiddle
添加回答
举报