-1

I have created a popup box of div. Which get shown when i click on an icon and get hide when i click on close button. But i want to get it closed if click out side of the popup. What i can do for that. I am using angular in my project and ng-show and ng-hide to show and hide the div. Any help is appreciable. Thanks in advance. html code

<p ng-click="openPopUP()">openPopUp</p>

<div ng-show="popup">
<p>helo</p>
<p ng-click="closePopUp()">Close</p>
</div>

Java script

$scope.openPopUP = function(){
$scope.popup = true;
}
$scope.closePopUp= function(){
$scope.popup = false;
}

I just want to close if some one click out side the div

Bittu Singh
  • 91
  • 1
  • 13

2 Answers2

-1

In your controller:

 angular.element(document.body).on('click', function() {
     $scope.$apply(function() {
         $scope.closePopUp();
     });
 });
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • 1
    Its working but the problem is that it get closed if i clicked on popup div as well as its in the body context as well. – Bittu Singh Jul 28 '14 at 06:29
-1

First of all read this post to get an idea how to close popups.

Fix for @pixelbits solution is to add $event.stopPropagation(); on popup div.

HTML

<p ng-click="openPopUP()">openPopUp</p>

<div ng-show="popup" ng-click="$event.stopPropagation();">
<p>helo</p>
<p ng-click="closePopUp()">Close</p>
</div>

JS

angular.element(document.body).on('click', function() {
     $scope.$apply(function() {
         $scope.closePopUp();
     });
 });

$scope.openPopUP = function(){
 $scope.popup = true;
}
$scope.closePopUp= function(){
 $scope.popup = false;
}
Community
  • 1
  • 1
waqas
  • 4,357
  • 3
  • 34
  • 42
  • Thanks @Waqas but it won't work because for every click we are clicking the body context and it will never make the popup to be showed, as with this code we are closing the popup always. Hope you got ma point. – Bittu Singh Feb 16 '15 at 06:20