3

我是 AngularJS 的新手。我正在使用 Twitter Bootstrap 进行个人项目,因此我可以学习 AngularJS。我的数据在 JSON 文件中。项目标题和主菜单链接填充得很好。在其中一个链接下有一个子菜单,这就是我无法填充的内容。我想做的是使用 ng-repeat 加载子菜单。所以基本上我正在处理 ng-repeat 中的 ng-repeat。欢迎任何和所有提示。谢谢!

我的控制器如下所示:

'use strict';

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

app.controller('NavCtrl', function($scope, $http) {

$http.get('app/content/nav.json').success(function(data) {

    $scope.nav   = data;
    $scope.links = data.links;

    });

});

我的 JSON:

{
"projectTitle" : "My Website Title",
"links" : [
    {"name" : "Home", "url" : "/", "className" : ""},
    {"name" : "About", "url" : "/about", "className" : ""},
    {"name" : "Contact", "url" : "/contact", "className" : ""},
    {"name" : "Categories", "url" : "/categories", "className" : "dropdown", "sub" :
        [
            {"name" : "Tech Stuff", "url" : "/techStuff"},
            {"name" : "AngularJS", "url" : "/angularJS"},
            {"name" : "HTML5", "url" : "/html5"},
            {"name" : "Javascript", "url" : "/javascript"},
            {"name" : "jQuery", "url" : "/jquery"}
        ]
    }
]
}

我的 HTML:

<div ng-controller="NavCtrl" class="navbar navbar-inverse navbar-fixed-top">
    <div class="navbar-inner">
        <div class="container">
            <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="brand" href="#">{{nav.projectTitle}}</a>
            <div class="nav-collapse collapse">
                <ul class="nav">
                    <li ng-repeat="link in links" class="{{link.className}}">
                        <a href="{{link.url}}" class="{{link.sub && 'dropdown-toggle' || ''}}" data-toggle="{{link.sub && 'dropdown' || ''}}">{{link.name}} 
                            <b ng-show="link.sub" class="caret"></b>
                        </a>
                        <ul class="dropdown-menu" ng-show="link.sub">
                            <li ng-repeat="sub in links">
                                <a href="">{{sub.name}}</a>
                            </li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
4

1 回答 1

4

将子菜单数据填充模板更改为

<li ng-repeat="subItem in link.sub">
    <a href="{{subItem.url}}">{{subItem.name}}</a>
</li>

link.sub是当前数据对象的子集合,然后循环遍历子集合的每一项。

Demo on jsFiddle

于 2013-08-04T03:38:53.467 回答