简体   繁体   English

外部点击在angularjs中不起作用

[英]outside click is not working in angularjs

I have a button. 我有一个按钮。 If i click that button one popup will open in Navbar. 如果我单击该按钮,将在导航栏中打开一个弹出窗口。 I am using ui-bootstrap for this. 我为此使用ui-bootstrap。

If I click outside the popup is not closing. 如果单击外部,则弹出窗口不会关闭。

  • I wrote window.onclick function to achieve this. 我写了window.onclick函数来实现这一点。 It is working. 这是工作。

  • But My problem is, If I click button popup will open. 但是我的问题是,如果我单击按钮,弹出窗口将打开。 If I select any value in dropdown, on top of that one more popup will open. 如果我在下拉列表中选择任何值,则将在该弹出窗口的上方打开另一个弹出窗口。

  • So If I select any dropdwon value on the second dropdown, the first dropdown is automatically closing. 因此,如果我在第二个下拉列表中选择任何dropdwon值,则第一个下拉列表会自动关闭。

  • My need is, When I click button dropdown should open. 我需要的是,当我单击按钮时,下拉列表应该打开。 and If I click outside the dropdown close. 如果我在下拉菜单外部单击,则关闭。

  • If I select any value from dropdown, the dropdown should stable. 如果我从下拉列表中选择任何值,则下拉列表应该稳定。 It should not go off. 它不应该熄灭。 Only when I click outside that time only it should go. 只有当我在该时间以外单击时,它才应该进入。

<button id="btn-append-to-body" type="button" class="btn btn-primary" ng-show="showFilterBtn" ng-click="showDropdown($event)">
<img src="assets/images/filterIcon.png">&nbsp&nbspFilter <span class="caret"></span>
</button>
<li class="nav-item dropdown">
  <div class="dropdown-menu">
    <div class="dropdown-item" ng-repeat="filter in filters">
      <div>{{filter.filterObject}}</div>
    </div>
  </div>
</li>
window.onclick = function() {
   $(".dropdown-menu").hide();
}

If I click button, the dropdown should not go. 如果单击按钮,则下拉列表不应消失。

This is how you can do, again this is not ideal way to do it, since we have little data provided by you, I can only think of this kind of fix :) 这就是您的操作方式,这也不是理想的方式,因为我们提供的数据很少,所以我只能想到这种修复方法:)

window.addEventListener("click", function(event) {
    if (!$(event.target).hasClass('dropdown-menu') &&!$(event.target).parents().hasClass('dropdown-menu')) {
         $(".dropdown-menu").hide();
    }
});

EDIT 1 编辑1

Just add a parent div and keep a unique class for dropdown-parents which you don't want to close suppose class name is dropdown-select 只需添加一个父div并为您不想关闭的下拉列表父项保留一个唯一的类,假设该类名是dropdown-select

and then just edit the condition like this 然后像这样编辑条件

if (!$(event.target).hasClass('dropdown-select') &&!$(event.target).parents().hasClass('dropdown-select')) {

You should add an extension to the jquery library then add this script since you are using javascript 您应该将扩展添加到jquery库,然后添加此脚本,因为您使用的是javascript

    <script>
        $( document ).ready(function() {
            $(".dropdown-menu").hide();
        });
    </script>

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

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