简体   繁体   English

angularjs关闭$ document上的模态窗口

[英]angularjs close modal window on $document click

I've been working on this all day and I'm stuck. 我一整天都在努力,而且我被困住了。 I have a modal/dialog that appears when a div is clicked (the div is in the th of a table im trying to create sorting options). 我有一个模式/对话框,当单击div时会出现(div位于表格中,我试图创建排序选项)。 The div fires ng-click and sets a $scope variable to true or false. div触发ng-click并将$ scope变量设置为true或false。 I need that modal to hide when anywhere outside the div is clicked. 当点击div之外的任何地方时,我需要隐藏模态。 In addition, there are multiple divs that show the modal. 此外,有多个div显示模态。 If another div is clicked, hide the current modal and show the modal again with that $scope. 如果单击另一个div,则隐藏当前模态并使用该$ scope再次显示模态。

<table>
 <thead>
  <th>Category 1 <div class="optionbox" ng-click="showModal($event)"></th>
  <th>Category 2 <div class="optionbox" ng-click="showModal($event)"></th>
  <th>Category 3 <div class="optionbox" ng-click="showModal($event)"></th>
 </thead>
</table>

I have a directive for the modal that watches that value set by showModal() and displays. 我有一个模式的指令,它监视由showModal()设置的值并显示。

$scope.showModal = function(event){
      $scope.isModalVisible = !$scope.isModalVisible;
}

The modal directive: 模态指令:

angular.modal('app', []).directive('modal', function(){
      return {
         templateUrl : ..., 
         restrict : 'E',
         link : function($scope, element, attrs){
            ...
            $scope.$watch('isModalVisible', function(newVal, oldVal){
             if(newVal){
                element.slideUp(1000);
              }else{
                element.slideDown(1000);
              }
           }
        }

The close modal directive (set as an attribute in the modal template) close模态指令(在模态模板中设置为属性)

angular.modal('app', []).directive('globalModalClose', function(){
      return {
         link : function($scope, element, attrs){
            $document.on('click', function(){
               if(element.find('event.target').length() < 1){
                 element.hide()
               }
            });
        }

I may be missing a few things here but here' my issue. 我可能在这里遗漏了一些东西,但这里是我的问题。 My click on the div shows the modal, then instantly closes it. 我点击div显示模态,然后立即关闭它。 This is because my showModal function is called before my $document.on listener. 这是因为我的$ document.on监听器之前调用了我的showModal函数。 Can I either 1)get the click listener to fire first or 2)something else... 我可以1)让点击监听器先开火,或者2)其他东西......

The click event bubbles up the DOM. click事件冒泡DOM。 As such, your $document 's click listener is receiving the same click event that opened the modal, after the modal is open. 因此,在模式打开后, $document的单击侦听器正在接收打开模式的相同单击事件。

You can use event.stopPropagation() to stop the event from bubbling further up the DOM to other click listeners. 您可以使用event.stopPropagation()来阻止事件从DOM进一步向上冒泡到其他单击侦听器。 Angular allows passing the $event into ng-click functions (as you can see in your HTML, where you are passing with with ng-click="showModal($event)" . Angular允许将$event传递给ng-click函数(正如您在HTML中看到的那样,您使用ng-click="showModal($event)"传递的内容ng-click="showModal($event)"

So, applied to your code, you can do: 因此,应用于您的代码,您可以:

$scope.showModal = function(event) {
  event.stopPropagation();
  $scope.isModalVisible = !$scope.isModalVisible;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM