angular下拉子菜单工作不正确

时间:2016-04-06 15:54:46

标签: javascript html angularjs drop-down-menu ng-repeat

所以我的下拉菜单中有几个错误,我无法解决。

  1. Ng-repeat为父下拉菜单中的每个元素设置相同的子菜单,即使我尝试通过范围函数或过滤器限制它
  2. 每次从数组加载最后一个对象时的函数。
  3. this is my plunker

    和我的代码

    HTML

    <div class="dropdown category" style="position:relative">
       <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
        <ul class="dropdown-menu">
           <li class="dropdown" ng-repeat="entity in entitis" >
               <a class="trigger right-caret">{{ entity }}</a>
       <ul ng-if="entity | filter : 'Main'" class="dropdown-menu sub-menu">
             <li ng-repeat="domain in ast" ng-click="getDomain()">
                <a href=""> {{domain.name}}</a>
        </li>
        </ul>
        </li>
       </ul>
     </div>
    {{ items }}
    

    JS

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope, $http) {
      $scope.name = 'Reinforcement';
    
      $scope.entitis = ["Main", "Submain", "Class"];
    
                for (var i = 0; i < $scope.entitis.length; i++){
                    if ($scope.entitis[i] == "Main"){
                        $http.get('ast.json')
                            .then(function (response) {
                                $scope.ast = response.data;
                                console.log($scope.ast);
                            });
                    }
                }
    
          $scope.getDomain = function () {
                for (var i = 0; i < $scope.ast.length; i++) {
                   $scope.items = $scope.ast[i].children
                }
             }
    
    
            $(function(){
            $(".dropdown-menu > li > a.trigger").on("click",function(e){
                var current=$(this).next();
                var grandparent=$(this).parent().parent();
                if($(this).hasClass('left-caret')||$(this).hasClass('right-caret'))
                    $(this).toggleClass('right-caret left-caret');
                grandparent.find('.left-caret').not(this).toggleClass('right-caret left-caret');
                grandparent.find(".sub-menu:visible").not(current).hide();
                current.toggle();
                e.stopPropagation();
            });
            $(".dropdown-menu > li > a:not(.trigger)").on("click",function(){
                var root=$(this).closest('.dropdown');
                root.find('.left-caret').toggleClass('right-caret left-caret');
                root.find('.sub-menu:visible').hide();
            });
        });
    
    });
    

    我感谢任何帮助或建议

1 个答案:

答案 0 :(得分:0)

我弄清楚它是如何运作的。的 this is working plunker

和代码

js

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

app.controller('MainCtrl', function($scope, $http) {
  $scope.name = 'Reinforcement';

  $scope.entitis = ["Main", "Submain", "Class"];

            for (var i = 0; i < $scope.entitis.length; i++){
                if ($scope.entitis[i] == "Main"){
                    $http.get('ast.json')
                         .success(function (data) {
                        $scope.ast = data;
                        });
                }
            }

      $scope.getDomain = function (index) {
           console.info($scope.ast[index].name);
            for (var i = 0; i < $scope.ast.length; i++) {
               $scope.items = $scope.ast[index].children
            }
         }


        $(function(){
        $(".dropdown-menu > li > a.trigger").on("click",function(e){
            var current=$(this).next();
            var grandparent=$(this).parent().parent();
            if($(this).hasClass('left-caret')||$(this).hasClass('right-caret'))
                $(this).toggleClass('right-caret left-caret');
            grandparent.find('.left-caret').not(this).toggleClass('right-caret left-caret');
            grandparent.find(".sub-menu:visible").not(current).hide();
            current.toggle();
            e.stopPropagation();
        });
        $(".dropdown-menu > li > a:not(.trigger)").on("click",function(){
            var root=$(this).closest('.dropdown');
            root.find('.left-caret').toggleClass('right-caret left-caret');
            root.find('.sub-menu:visible').hide();
        });
    });


});

HTML

<div class="dropdown category" style="position:relative">
                                    <a href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> Category <span class="caret"></span></a>
                                    <ul class="dropdown-menu">
                                        <li class="dropdown" ng-repeat="entity in entitis" >
                                            <a class="trigger right-caret">{{ entity }}</a>
                                            <ul ng-if="entity == 'Main'" class="dropdown-menu sub-menu">
                                                <li ng-repeat="domain in ast" ng-click="getDomain($index)">
                                                    <a href=""> {{domain.name}}</a>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </div>

                                {{ items }}