javascript - How to close dynamically generated dropdown on clicking on the body using angularjs? -


hi developing web application in angularjs. on top right corner have 1 drop-down box , become active after user logged in. have scenario user clicks on dropdown , once clicks anywhere on page want close dropdown. struggling fix long time. have created plunker in order understand problem facing properly.

`https://plnkr.co/edit/4rjrbdhe6mmxofwrovfb?p=preview` 

may here fix this? appreciated!. thank you...

you can use $event.stoppropagation. can add following code make work

html:

<div class="user" ng-click="opendropdown($event)">    <h1>dropdown</h1>    <div class="user-dropdown" ng-show="dp" id="profiledropdown"></div> </div> 

js:

$scope.opendropdown = function(event) {     if ($scope.dp === false || $scope.dp === undefined) {         $scope.dp = true;         event.stoppropagation();     } else {         $scope.dp = false;     } }; window.onclick = function() {     if ($scope.dp) {         $scope.dp = false;         $scope.$apply();     } }; 

working demo: https://plnkr.co/edit/6h91nxc4s8zd7jaboz33?p=preview

if want use directive achieve this, check out solution


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -