0

I am using bs-popover to display my contents on click(as a menu) in angularjs. But I need to hide this popover-menu when I click somewhere in the browser window. I want it to be dismissed on that type of event. How can I do that?

4

2 回答 2

1

您需要为此编写指令。

yourApp.directive('bndocumentclick',
 function($document,$rootScope,$timeout) {
  return {
    restrict: 'EA',
     link : function(scope, element, attrs) {
        $document.on("click", function(ev) {
           // Do stuff here to remove your popover.
        }
     }
 }
});

HTML

<body bndocumentclick>

<div bs-popover ng-click="$event.stopPropagation()">

您需要使用,因为您不想在用户单击弹出框内时关闭弹出框。

于 2013-08-21T10:48:03.880 回答
0

@Jay Shukla 提供的解决方案不起作用。

当您在弹出框内单击时,触发弹出框的元素上的“$event.stopPropagation()”不会阻止它关闭。如果您在弹出框内进行了一些交互,这将是一个问题。

这有效:

angular.module('yourApp')
.directive('closePopovers', function ($document, $rootScope, $timeout) {
return {
  restrict: 'EA',
  link: function (scope, element, attrs) {
    $document.on('click', function (ev) {

      var targetElem = angular.element(ev.target);

      if (targetElem.data('toggle') !== 'popover' 
      && targetElem.parents('[data-toggle="popover"]').length === 0 
      && targetElem.parents('.popover').length === 0) {

        $('.popover').each(function () {
            var $this = $(this);
            var scope = $this.scope();
            scope.$apply(function () {
              scope.$hide();
            });
          }
        );
      }
    });
  }
};
});

在你的身体上:

在触发弹出窗口的元素上:

<button data-toggle="popover" [other data elements here] bs-popover>Toggle popover</button>
于 2014-04-03T07:35:50.823 回答