Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close all Angular JS Bootstrap popovers with click anywhere on screen?

I am using the Angular directives for bootstrap.

I have a popover as in their example:

<button popover="Hello, World!" popover-title="Title" class="btn btn-default ng-scope">Dynamic Popover</button>

It closes when you click on the button again. I'd like to close it -- and any other open popovers -- when the user clicks anywhere.

I don't see a built-in way to do this.

like image 204
Steve Avatar asked Apr 07 '14 15:04

Steve


People also ask

How do I destroy bootstrap popover?

Use the popver(“detroy”) to destroy the popover.

How do you close popover?

To hide the displayed popover, use the popover(“hide”) method.

How do I show popover on hover?

Set the trigger option of the popover to hover instead of click , which is the default one. Or with an initialization option: $("#popover"). popover({ trigger: "hover" });


3 Answers

angular.element(document.body).bind('click', function (e) {
    var popups = document.querySelectorAll('.popover');
    if(popups) {
        for(var i=0; i<popups.length; i++) {
            var popup = popups[i];
            var popupElement = angular.element(popup);

            if(popupElement[0].previousSibling!=e.target){
                popupElement.scope().$parent.isOpen=false;
                popupElement.remove();
            }
        }
    }
});
like image 188
Lauren Campregher Avatar answered Nov 04 '22 18:11

Lauren Campregher


This feature request is being tracked (https://github.com/angular-ui/bootstrap/issues/618). Similar to aet's answer, you can do what is recommended in the feature request as a work-around:

$('body').on('click', function (e) {
   $('*[popover]').each(function () {
        //Only do this for all popovers other than the current one that cause this event
        if (!($(this).is(e.target) || $(this).has(e.target).length > 0) 
             && $(this).siblings('.popover').length !== 0 
             && $(this).siblings('.popover').has(e.target).length === 0)                  
        {
             //Remove the popover element from the DOM          
             $(this).siblings('.popover').remove();
             //Set the state of the popover in the scope to reflect this          
             angular.element(this).scope().tt_isOpen = false;
        }
    });
});

(source: vchatterji's comment in feature request mentioned above)

The feature request also has a non-jQuery solution as well as this plnkr: http://plnkr.co/edit/fhsy4V

like image 21
sonara Avatar answered Nov 04 '22 17:11

sonara


        angular.element(document.body).bind('click', function (e) {
            var popups = document.querySelectorAll('.popover');
            if (popups) {
                for (var i = 0; i < popups.length; i++) {
                    var popup = popups[i];
                    var popupElement = angular.element(popup);
                    console.log(2);
                    if (popupElement[0].previousSibling != e.target) {
                        popupElement.scope().$parent.isOpen = false;
                        popupElement.scope().$parent.$apply();
                    }
                }
            }
        });
like image 35
Melian Galovic Avatar answered Nov 04 '22 19:11

Melian Galovic