[angularjs]ng-repeat (做) 的输出 json 文件带有下拉子菜单

发布时间: 2017/4/16 4:16:44
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想设置为下拉列表菜单的子菜单,该子菜单应该来自输出 json 文件,这是呆瓜的例子,第一个层次从 json 对象范围从生成,应该从 json 文件生成子菜单。在控制台中我看到该控制器看到我的 json 文件和对象内的但由于某种原因,它不是呈现子菜单,在这里是我的错吗?我感谢任何帮助。

我的代码︰

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

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

  $scope.entities = ["Main", "Submain", "Class"];
   $scope.a1 = false;

        $scope.getEnt = function(){
            "use strict";
            for (var i = 0; i < $scope.entities.length; i++){
                if ($scope.entities[i] == "Main"){
                    // $scope.a1 = true;
                    console.log($scope.ast);
                    console.log("check");


         $http.get('ast.json')
                        .then(function (response) {
                            $scope.ast = response.data;
                            console.log($scope.ast);
                        });

      $scope.getDomain = function () {
            "use strict";
            for (var i = 0; i < $scope.ast.length; i++) {
              alert($scope.ast[i].children)
            }
         }
                }
            }
        };

});

html:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.9/angular.js" data-semver="1.4.9"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Hello {{name}}!</p>

        <div class="dropdown category">
                                    <button class="btn btn-default dropdown-toggle" type="button" id="entity"
                                            data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                        Category <span class="caret"></span>
                                    </button>
                                    <ul class="dropdown-menu">
                                        <li class="dropdown dropdown-submenu" ng-repeat="entity in entities"
                                            ng-click="getEnt()"><a href="#" class="dropdown-toggle"
                                                                   data-toggle="dropdown">{{ entity }}</a>
                                            <ul class="dropdown-menu"
                                                aria-labelledby="ast1Domain">
                                                <li ng-repeat="domain in ast" ng-click="getDomain()">
                                                    <a href=""> {{domain.name}}</a>
                                                </li>
                                            </ul>
                                        </li>
                                    </ul>
                                </div>
  </body>

</html>

json

[
        {
          "name": "Demographics",
          "children": [
            {
              "name": "Study 1",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 2",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 3",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 3",
              "ORIGIN": "Assigned"
            }]
        },
         {
          "name": "Monopoly",
          "children": [
            {
              "name": "Study 1",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 2",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 3",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 3",
              "ORIGIN": "Assigned"
            }]
        },
         {
          "name": "World",
          "children": [
            {
              "name": "Study 1",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 2",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 3",
              "ORIGIN": "Assigned"
            },
             {
              "name": "Study 3",
              "ORIGIN": "Assigned"
            }]
        }
        ]

解决方法 1:

我不知道是否这是正是你想要什么,但基本上使用 dropdown-menu 类里面 dropdown-menu 类,和 bootstrap 未设置来处理这个,据我所知。所以你需要查找或创建一些自定义的 css 来处理这一切。这里有一个例子- https://plnkr.co/edit/Rl4JtHlBqGgZyQ47dSPn?p=preview

.dropdown-submenu {
    position:relative;
}
.dropdown-submenu>.dropdown-menu {
    top:0;
    left:100%;
    margin-top:-6px;
    margin-left:-1px;
    -webkit-border-radius:0 6px 6px 6px;
    -moz-border-radius:0 6px 6px 6px;
    border-radius:0 6px 6px 6px;
}

制作自定义类, dropdown-submenu 为您的用途和相应应用 css。您可以更改此成任何你喜欢的 !

官方微信
官方QQ群
31647020