Angular js使用json创建动态菜单

时间:2015-06-20 18:13:53

标签: angularjs html5

我是角度js的新手。我需要使用角度js创建动态菜单和超链接。 我有来自json的菜单名称和超链接,我需要显示。 目前我已尝试使用静态菜单。

我的菜单结构就像

Home

Services

        -ser1
        -ser2
        -ser3

About

    -abt1

Contact

所有菜单值和超链接都来自json文件。

这是我的json

 [
    {
        "id": 100,
        "product": 0,
        "childs": [
            {
                "id": 200,
                "description": {
                    "id": 0,
                    "name": "Home",
                    "url": "home"
                }
            }
        ]
    },
    {
        "id": 100,
        "description": {
            "id": 0,
            "name": "services",
            "url": "services"
        },
        "parent": null,
        "childs": [
            {
                "id": 200,
                "description": {
                    "id": 0,
                    "name": "Ser1",
                    "url": "Ser1"
                },
                "productCount": 0,
                "childs": [
                    {
                        "id": 250,
                        "description": {
                            "id": 0,
                            "name": "ser2",
                            "url": "Ser2"
                        },
                        "childs": []
                    },
                    {
                        "id": 251,
                        "description": {
                            "id": 0,
                            "name": "ser3",
                            "url": "ser3"
                        },
                        "productCount": 0,
                        "childs": []
                    }
                ]
            }
        ]
    },
    {
        "id": 201,
        "description": {
            "id": 0,
            "name": "About",
            "url": "about"
        },
        "productCount": 0,
        "childs": [
            {
                "id": 203,
                "description": {
                    "id": 0,
                    "name": "abt1",
                    "url": "underground"
                },
                "productCount": 0,
                "childs": []
            }
        ]
    },
    {
        "id": 202,
        "description": {
            "id": 0,
            "name": "Contact",
            "url": "con"
        },
        "productCount": 0,
        "childs": []
    }
]

这是我的HTML

<li class="prod-dropdown" ng-repeat="menu in menus" ng-class="{proddropdown: menu.menus}">
                    <a ng-href="#/{{menu.action}}" ng-class="{'dropdown-toggle': menu.menus}"
                       data-toggle="dropdown">{{menu.menus.desc['name']}} </a>
                    <ul ng-if="menu.menus" class="dropdown-menu">
                        <li ng-repeat="submenu in menu.menus">
                            <a ng-href="#/{{submenu.action}}">{{submenu.desc}}</a>
                        </li>
                    </ul>
                </li>

4 个答案:

答案 0 :(得分:1)

您应该使用 ng-include 属性 - http://benfoster.io/blog/angularjs-recursive-templates

答案 1 :(得分:1)

2级子菜单

<li class="prod-dropdown" ng-repeat="menu in menus">
            <a ng-href="#/{{menu.description['url']}}" ng-class="{'dropdown-toggle': menu.menus}" class="dropdown-toggle" data-toggle="dropdown">
                {{menu.description['name'] | uppercase}} <span ng-if="menu.childs" class="caret"></span>
            </a>
            <ul ng-if="menu.childs" class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                <li class="dropdown-submenu" ng-repeat="submenu in menu.childs">
                    <a tabindex="-1" ng-href="#/products/{{submenu.description['url']}}">{{submenu
                        .description['name']}}</a>
                    <ul class="dropdown-menu" ng-if="submenu.childs">
                        <li class="dropdown-submenu" ng-repeat="subofsub in submenu.childs">
                            <a ng-href="#/products/{{subofsub.description['url']}}">{{subofsub.description['name']}}</a>
                        </li>
                    </ul>
                </li>
            </ul>
    </li>

答案 2 :(得分:0)

以下是&#34;动态多级菜单&#34;

的示例

<强> HTML

    <ul class="sidebar-menu" ng-repeat="m in modulos">
      <li class="treeview" ng-repeat="(itemIndex, item) in modulos">
        <a ng-click="showSubmenu(itemIndex)">
          <i class="fa fa-table"></i> <span>{{item.module}}</span>
        </a>

        <ul class="sub-nav" ng-show="isShowing(itemIndex)">
          <li ng-repeat="sub_element in m.submodule">
            <a href="{{sub_element.url}}">{{sub_element.res}}</a>
          </li>
        </ul>
      </li>
    </ul>

<强>的Javascript

    $scope.showSubmenu = function(item) {
        if ($scope.activeParentIndex === item) {
          $scope.activeParentIndex = "";
        } else {
          $scope.activeParentIndex = item;
        }
      }

      $scope.isShowing = function(index) {
        if ($scope.activeParentIndex === index) {
          return true;
        } else {
          return false;
        }
      };

      $scope.modulos = [{
        "module": "Admin System ",
        "adm_modulo_id": 1,
        "submodule": [{
          "res": "Angular",
          "url": "#/admin/1"
        }, {
          "res": "Linux",
          "url": "#/admin/2"
        }, {
          "res": "Server",
          "url": "#/admin/3"
        }]

      }];

,结果显示在Plunk

答案 3 :(得分:0)

回顾这个页面http://benfoster.io/blog/angularjs-recursive-templates,他们自json结构以来创建了一个多层菜单,无论你有多少级别。示例http://jsfiddle.net/NP7P5/1719/

<div ng-app="app" ng-controller='AppCtrl'>
    <script type="text/ng-template" id="categoryTree">
        {{ category.title }}
        <ul ng-if="category.categories">
            <li ng-repeat="category in category.categories" ng-include="'categoryTree'">           
            </li>
        </ul>
    </script>
    <ul>
        <li ng-repeat="category in categories" ng-include="'categoryTree'"></li>
    </ul>
</div>
var app = angular.module("app", []);
app.controller("AppCtrl", function ($scope) {
$scope.categories = [
  { 
    title: "Computers",
    categories: [
      {
        title: "Laptops",
        categories: [
          {
            title: "Ultrabooks"
          },
          {
            title: "Macbooks"
          }
        ]
      },
      {
        title: "Desktops"
      },
      {
        title: "Tablets",
        categories: [
          { 
            title: "Apple"
          },
          {
            title: "Android"
          }
        ]        
      }
    ]
  },
  {
    title: "Printers"
  }
];
});