Close AngularStrap popover

↘锁芯ラ 提交于 2019-12-09 00:41:06

问题


When I click a button, it appears a popover which can be closed if you click the button inside the popover. However, if you click another button to open a popover, you will see two popovers at the same time and I want to keep just one.

I have tried with the trigger 'focus', but it closes the popover if I click in the textarea inside the popover, I expected that this trigger was called when you click outside of the popover.

Any idea for this? Can the methods $hide, $show be called from the controller?


回答1:


OK, I am pretty sure this is a terrible hack, but here goes. Assuming your popover templates all use the popover class (if you aren't using a custom template with the data-template attribute, they should), and they're siblings of the button that triggers them (if you haven't mucked with the container attribute, they are), you can apply the following directive to your popover buttons. Note: this assumes that the parent elements of your popovers and popover buttons have unique ids.

angular.module('yourApp.directives', []).directive('rmPopovers',
    function($document,$rootScope,$timeout,$popover) {
        return {
            restrict: 'EA',
            link : function(scope, element, attrs) {
                var $element = $(element);
                $element.click(function() {
                    $('.popover').each(function(){
                        var $this = $(this);
                        if($this.parent().attr('id') != $element.parent().attr('id'))
                        {
                            $this.scope().$hide();
                        }
                    }
                    );
                });
            }
        }
    }
);

And then

<button type="button" bs-popover rm-popovers [your data attribs here]>Button!</button



回答2:


Try to add ng-click="$hide()" on the dismissable element of the popover. I.E.:

<a class="btn btn-primary"  ng-click="$hide()">Close</a>

It's not included in the documentation but it works for me, iff you use data-template for popover content, haven't tried with other opened popover yet.




回答3:


This should be an old question, in latest version of angular-strap, a new attribute could be used in this case: auto-close='1'




回答4:


There is an issue in the angular-strap github project which asks exactly the feature you want.

Nevertheless, at the moment I'm writing this answer, it's still open.




回答5:


trigger : 'focus' ,

worked for me on the same issue



来源:https://stackoverflow.com/questions/22020775/close-angularstrap-popover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!