如何在导航栏外单击隐藏子菜单

时间:2015-04-02 16:25:18

标签: javascript angularjs

我想写一个指令来生成导航栏。 https://jsfiddle.net/berber5/p03uhub8/

index.html:

<html lang="fr" ng-app="activity" id="ng-app">    
    <div  ng-controller="mainCtrl">
        <navigation-bar datas ="data"> </navigation-bar>
    </div>
</html>

我的指示:

app.directive('navigationBar', ['$window', function ($window) {

        return {
            restrict: 'EA',
            template: "<div id='navigation' class='navigation'>"+
                "<ul id='navidationUl' name='navigationName'> "+
                    "<li id='navLi' ng-repeat='(key, value) in datas'>"+
                        "<a  ng-href='{{value.route}}' ng-click='toggle($index)'> {{value.libelle}} "+
                            "<span ng-if='value.subMenu.length > 0' class='caret'></span>"+
                        "</a> "+
                        "<ul  id ='subMenu' ng-show ='value.showMe && value.subMenu.length > 0'>"+
                            "<li ng-repeat='(key2, value2) in value.subMenu'>"+
                                "<a ng-href='{{value2.route}}' ng-click ='dismissAll()'>{{value2.libelle}}</a>"+
                            "</li>"+
                        "</ul>"+

                    "</li> "+
                "</ul>"+
            "</div>",
            scope: {
                datas : "=datas"
            },
            link: function (scope, elem, attrs) {
                console.log("LINK : ", scope, elem, attrs);
                scope.dismissAll = function (){
                    for(var i = 0; i< scope.datas.length; i++) {
                        scope.datas[i].showMe = false;
                    }
                }
                scope.toggle = function toggle(index){
                    scope.datas[index].showMe = !scope.datas[index].showMe;
                    for(var i = 0; i< scope.datas.length; i++) {
                        if (i !=index) {
                            scope.datas[i].showMe = false;
                        }
                    }
                };
            }
        };
}]);

如果用户在导航栏外单击

,我会寻找隐藏子菜单的方法

1 个答案:

答案 0 :(得分:1)

您需要添加全局事件点击以隐藏所有菜单:

app.directive('navigationBar', ['$window', '$document', function ($window, $document) {

    return {
        restrict: 'EA',
        template: "<div id='navigation' class='navigation'>"+
            "<ul id='navidationUl' name='navigationName'> "+
                "<li id='navLi' ng-repeat='(key, value) in datas'>"+
                    "<a  ng-href='{{value.route}}' ng-click='toggle($index)'> {{value.libelle}} "+
                        "<span ng-if='value.subMenu.length > 0' class='caret'></span>"+
                    "</a> "+
                    "<ul  id ='subMenu' ng-show ='value.showMe && value.subMenu.length > 0'>"+
                        "<li ng-repeat='(key2, value2) in value.subMenu'>"+
                            "<a ng-href='{{value2.route}}' ng-click ='dismissAll()'>{{value2.libelle}}</a>"+
                        "</li>"+
                    "</ul>"+

                "</li> "+
            "</ul>"+
        "</div>",
        scope: {
            datas : "=datas"
        },
        link: function (scope, elem, attrs) {
            scope.dismissAll = function (){
                for(var i = 0; i< scope.datas.length; i++) {
                    scope.datas[i].showMe = false;
                }
            }
            scope.toggle = function toggle(index){
                scope.datas[index].showMe = !scope.datas[index].showMe;
                for(var i = 0; i< scope.datas.length; i++) {
                    if (i !=index) {
                        scope.datas[i].showMe = false;
                    }
                }
            };

            elem.bind('click', function(e) {
                // this part keeps it from firing the click on the document.
                e.stopPropagation();
            });
            $document.bind('click', function() {
                scope.$apply(scope.dismissAll);
            })
        }
    };
}]);

此处提供更多信息:Angular Click outside of an element event