简体   繁体   English

AngularJS-在多个元素上使用ng-click和ng-class时的奇怪行为

[英]AngularJS - Strange behavior when using ng-click and ng-class on multiple elements

I wasn't sure of the best way to term this question. 我不确定解决这个问题的最佳方法。

I'm using ui.bootstrap and have several dropdowns in my navbar. 我正在使用ui.bootstrap,并且在我的导航栏中有几个下拉菜单。 I have a nice animation on these navbar dropdowns. 这些导航栏下拉菜单上有一个不错的动画。 They're all basically like this one: 他们基本上都是这样的:

<div id="settings_menu" dropdown data-ng-click="ddAnimate()">
    <span class="glyphicons glyphicons-settings dropdown-toggle cursor" data-toggle="dropdown"></span>
    <ul class="dropdown-menu" role="menu" data-ng-class="ddGetClass()">
        <li><a href="javascript:void(0);"><span class="glyphicons glyphicons-bell text-purple mr15"></span>Users</a></li>
        <li><a href="javascript:void(0);"><span class="glyphicons glyphicons-notes text-blue mr15"></span>Servers</a></li>
        <li><a href="javascript:void(0);"><span class="glyphicons glyphicons-facebook text-orange mr15"></span>Crons</a></li>
    </ul>
</div>

So notice the ddAnimate() on the parent div and the ddGetClass() on the child UL element. 所以注意父DIV的ddAnimate()和UL子元素上ddGetClass()。

Here's the controller: 这是控制器:

'use strict'

angular.module('tbads').controller('TopNavController', ['$scope',

    function($scope) {
        var flag = true;
        $scope.ddGetClass = function() {
            return flag ? "": "animated animated-shortest flipInX";
        }

        $scope.ddAnimate = function() {
            flag = !flag;
        }
    }

]);

Ok so quite simply, this works well, as long as you're following a specific pattern of clicking. 好吧,很简单,只要您遵循特定的点击模式,这就可以很好地工作。 For example, and keep in mind I have several of these on the header, with their own Id's, etc. 例如,请记住,我在标头上有几个,带有自己的ID,等等。

If I click on one of them, the animation takes place as the class is appended with ng-class. 如果单击其中之一,动画将在该类附加ng-class时发生。 If I click it again, it closes and the class is removed. 如果再次单击它,它将关闭并删除该类。 If I click it again, it continues to operate just fine. 如果再次单击它,它将继续正常运行。 The problem is, if I click on one dropdown, it animates, and if I click on another dropdown or whitespace instead of closing the first one, the first one's class is never removed, so that if I go back to it and click it again, the animation is not present. 问题是,如果我单击一个下拉列表,则会对其进行动画处理;如果我单击另一个下拉列表或空白而不是关闭第一个下拉列表或空白,则永远不会删除第一个下拉列表或班级,因此,如果我回到它并再次单击它,则动画不存在。

My basic problem is I need to understand how to remove the animate class on all other dropdowns when another dropdown is clicked? 我的基本问题是,当单击另一个下拉菜单时,我需要了解如何删除所有其他下拉菜单的动画类? I'm sure I'm doing something wrong, or perhaps there's a better way. 我确定我做错了什么,或者也许有更好的方法。

Regardless, I need that class removed if the dropdown is not open. 无论如何,如果下拉列表未打开,则需要删除该类。

Thank you for suggestions. 谢谢你的建议。

One solution would be to use $interval to check whether the dropdown is open (has an 'open' class). 一种解决方案是使用$ interval检查下拉列表是否打开(具有“ open”类)。 If it's not open, then remove the animation classes. 如果未打开,则删除动画类。

$interval(function()
{
   if(! jQuery("#settings_menu").hasClass("open")))
   {
      jQuery("ul.dropdown-menu").removeClass( "animated animated-shortest flipInX" )
   }
});

Don't forget to inject $interval into your controller. 不要忘记将$ interval注入到控制器中。 Since you are using multiple dropdowns, each surrounding div and inner ul will need unique id's. 由于您使用的是多个下拉菜单,因此每个周围的div和内部ul将需要唯一的ID。 If you are using ng-repeat to produce your dropdowns, you can use $index to generate unique id's. 如果您使用ng-repeat生成下拉列表,则可以使用$ index生成唯一的ID。

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

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