我无法解释你原来问题的真正原因。似乎 ng-click on body 标签不是一个好主意 - 我认为它在某些方面会窃取焦点..
我已经组合了一个复杂的解决方案 - 但它适用于桌面和模拟移动设备 - 在 Firefox 中测试 - 并处理“点击+触摸”问题:http:
//jsfiddle.net/s_light/L85g3grs/6/
在按钮上设置点击事件:
<button type="button" ng-click="menuShow($event)">
Show menu
</button>
并在您的控制器中添加处理:
app.controller('MainController',[
'$scope',
'$document',
'$timeout',
function($scope, $document, $timeout) {
// using deep value so that there are no scope/childscope issues
$scope.menu = {
visible: false,
};
// our internal clickPrevent helper
var menu_clickPrevent = false;
function menuHide(event) {
console.log("menuHide");
// set menu visibility
$scope.menu.visible = false;
// we need a apply here so the view gets updated.
$scope.$apply();
// deactivate handler
$document.off('click', menuHide);
}
$scope.menuShow = function(event) {
console.log("menuShow", event);
// check if we are already handling a click...
if( !menu_clickPrevent ) {
// stop default and propagation so our hide handler is not called immediate
event.preventDefault();
event.stopPropagation();
// make menu visible
$scope.menu.visible = true;
// prevent 'double click' bugs on some touch devices
menu_clickPrevent = true;
$timeout(function () {
menu_clickPrevent = false;
}, 100);
// activate document wide click-handler
$document.on('click', menuHide);
}
};
}
]);