Implementing a simple dropdown directive that uses ng-show to show or hide a menu is trivial in AngularJS. I want to also close the menu if the user clicks elsewhere in the screen after having opened the menu. What is the simplest possible correct way to do this? I have tried using $document.bind and $document.unbind to unbind from the document afterwards, but it does not seem to work as expected, and I can't work ought how it ought to work due to questions of scoping.
I'm aware the ui-bootstrap has a similar directive (dropdownToggle), and I've dug through the source of it, but it looks much more complicated and introduces a dependency on an older version of bootstrap.
Here is an example plunker (that does not close the menu on click elsewhere).
Code:
<!DOCTYPE html>
<html ng-app="plunker" >
<head>
<link data-require="[email protected]" data-semver="3.0.0" rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js"></script>
<script src="example.js"></script>
<link href="///netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div ng-app="dropdown">
<div menu-status-widget></div>
</div>
</body>
</html>
ng-click. WHen using external events that angular isn't aware of to change scope, you have to change the scope within$.applywhich triggers digest . Once you do this using the'ng-clcikapproach, the menu never open...the 2 cancel each otherng-click,