我有一个用 AngularJS 创建的应用程序,它有一条菜单。我想做的是为每个菜单创建子菜单。<ul> <li> ... </li> </ul>
可以通过在我现有的菜单列表项下添加其他内容来创建子菜单。我该怎么做?
这是我的 AngularJS 调用代码:
<div id="menu">
<ul>
<li ng-class="{selected: $index==currPage}" ng-repeat="page in data.pages">
<a href="" ng-click="goToPage($index)">{{page.menuTitle}}</a> {{page.subMenu}}
</li>
</ul>
</div>
这会在 HTML 中生成以下代码,其中 div 为id=menu
:
<ul>
<li class="ng-scope ng-binding selected" ng-class="{selected: $index==currPage}" ng-repeat="page in data.pages">
<a class="ng-binding" href="" ng-click="goToPage($index)">Introduction</a>
</li>
<li class="ng-scope ng-binding" ng-class="{selected: $index==currPage}" ng-repeat="page in data.pages">
<a class="ng-binding" href="" ng-click="goToPage($index)">Cases</a>
</li>
...
...
</ul>
AngularJS JS 代码
补充 HTML 之上的 Angular JS 代码是:
data.title = 'Amanpour consult Petrochemicals & Energy';
data.pages = [];
data.pages[0] = {};
data.pages[0].menuTitle = 'Introduction';
data.pages[0].slides = [];
data.pages[0].slides[0] = {heading:'A Heading: profile', speaker: 'The man himself', title:'Expert in awesomeness', img:'showing-awesomeness.jpg', video:'witnessing-awesomeness.m4v'};
....
data.pages[0].slides[1]
....
data.pages[0].slides[2]
....
data.pages[0].slides[3]
现在要添加子菜单,我想我可以跟随 AngularJS 代码:
data.pages[0].subMenu = '<ul> <li> <a href="#">First Link</a> </li> <li> <a href="#">Second Link</a> </li> </ul>';
然后像这样在 HTML 中调用它(注意{{page.subMenu}}
):
<li ng-class="{selected: $index==currPage}" ng-repeat="page in data.pages">
<a href="" ng-click="goToPage($index)">{{page.menuTitle}}</a> **{{page.subMenu}}**
</li>
但它不起作用,它给我的只是以下内容,它只是在页面上逐字打印 HTML:
<ul> <li> <a href="#">First Link</a> </li> <li> <a href="#">Second Link</a> </li> </ul>
更新
我必须像这样在 JS 中创建一个菜单:
data.subMenu = [];
data.subMenu[0] = {};
data.subMenu[0].list = [];
data.subMenu[0].list[0] = 'Menu 1';
data.subMenu[0].list[1] = 'Menu 2';
data.subMenu[0].list[2] = 'Menu 3';
并且必须像这样在我的 HTML 中调用它:
<div id="menu">
<ul>
<li ng-class="{selected: $index==currPage}" ng-repeat="page in data.pages">
<a href="" ng-click="goToPage($index)">{{page.menuTitle}}</a>
<ul>
<li ng-repeat="smenu in data.subMenu">
<a href="" ng-click="goToPage($index)">{{smenu.list[smenu]}}</a>
</li>
</ul>
</li>
</ul>
</div>
唯一的问题是我必须正确循环;我无法正确地在循环内创建索引。