None
None

Reputation: 9247

How to hide div clicking anywhere on page?

Ok i have main header controller where i have this:

<div class="fade-show-hide" ng-class="{'ng-hide':loggedInClose==true}" ng-show="loggedInOpened" ng-cloak>
@Html.Partial("~/Views/Shared/_LoggedInPartial.cshtml")
</div>

In script i have this:

$scope.loggedInClose = false;

  $scope.toggleLoggedIn = function () {
        $scope.loggedInOpened = !$scope.loggedInOpened;
        $scope.languagesOpened = false;
        $scope.loginOpened = false;  
    };

Problem is when i use this code nothing happend...i get $scope.loggedInClose = true; but div is not hide ...

angular.element(document).on('click', function () {

    $scope.loggedInClose = true;

});

Can someone explain me how to hide div if i click anywhere on page that?

Upvotes: 7

Views: 3123

Answers (2)

Nikhil Batra
Nikhil Batra

Reputation: 3148

Use ng-hide separately instead of using it inside ng-class. So change the HTML to:

<div class="fade-show-hide" ng-hide="loggedInClose" ng-show="loggedInOpened" ng-cloak>

I think your changes are not getting applied:

use $scope.$apply(); after $scope.loggedInClose = true;

See the plunkr "http://plnkr.co/edit/0u7u4InJsJpI4g4E8vvg?p=preview"

Upvotes: 1

dfsq
dfsq

Reputation: 193271

I hope you put this event listener in directive, not in controller? :) Anyway, you need to tel Angular to update scope bindings. For this call $apply method to kick off new digest:

angular.element(document).on('click', function () {
    $scope.loggedInClose = true;
    $scope.$apply();
});

Upvotes: 1

Related Questions