在ng-单击时切换导航菜单选项

时间:2015-08-29 17:24:19

标签: jquery angularjs

我想为菜单下拉菜单构建简单的切换菜单功能。我从堆栈帖子中获取了一些代码,但它与标签功能更相关。

那么我们怎样才能做到这一点。如果我为此目的使用jquery,会不会有任何问题。

http://jsfiddle.net/3G7Kd/107/

<div ng-app='app' class="filters_ct" ng-controller="selectFilter">
    <ul class="nav">
        <li ng-repeat="filter in filters" ng-click="select($index)" ng-class="{sel: $index == selected}">
            <span class="filters_ct_status"></span>
            {{filter.name}}

            <ul class="subul" ng-if=filter.lists.length>
                <li ng-repeat="list in filter.lists">
                    {{list}}
                </li>
            </ul>
        </li>
    </ul>
</div>

var app = angular.module('app', []); 

app.controller('selectFilter', function($scope) {

    $scope.filters = [
            {
                "name": "service",
                'lists': ['service a','service b','service c','service d']
            },
            {
                'name': "about us",
                'lists': ['about us a','about us b','about us c','about us d']
            },
            {
                'name': "product",
                'lists': ['product a','product b','product c','product d']
            },
            {
                'name': "more",
                'lists': ['more a','more b','more c','more d']
            }
        ];
    $scope.selected = 0;

    $scope.select= function(index) {
       $scope.selected = index; 
    };
});

1 个答案:

答案 0 :(得分:3)

您可能只想使用ng-click更改ng-mouseover

ng-mouseover="select($index)"

我还添加ng-mouseleave以在您离开导航元素

时删除任何选择

如果您需要点击并希望切换点击(显示/隐藏),则必须将$scope.select更改为:

$scope.select= function(index) {
   if ($scope.selected === index) 
       $scope.selected = null
   else
       $scope.selected = index; 
};

切换点击JSFiddle

鼠标悬停JSFiddle