I am defining a directive that builds a menu by interpreting a list of menu entries defined as objects in an array.
The menu opens and displays fine. It's a pop-over menu: it shows up in the foreground above whatever part of the page that it needs to cover. So far, so good.
However, I want to hide the menu as soon as the user clicks outside the menu. For that, I've used answers to this question to build this:
$scope.closeOnClick = function($scope) {
var $menu = $('#menu-view');
$(document).on('click', function(event) {
if (!$(event.target).closest('#menu-view').length) {
if ($menu.is(":visible")) {
$scope.hideMenu();
}
}
});
}
hideMenu
is a function defined in the controller of a toggle button that controls opening and closing the menu.
This closeOnClick
function is defined in that very controller. It is also called there so as to "install" the click handler.
Unfortunately, it seems that the $scope.hideMenu()
is ineffective and no errors are reported in the console. I have also tried using Angular events, with broadcast/on but to no avail.
I'm looking for ideas. Thank you.