AngularJS templates 递归循环
使用 ng-include 进行递归循环
数据结构
$scope.categories = [
{
title: 'Computers',
categories: [
{
title: 'Laptops',
categories: [
{
title: 'Ultrabooks'
},
{
title: 'Macbooks'
}
]
},
{
title: 'Desktops'
},
{
title: 'Tablets',
categories: [
{
title: 'Apple'
},
{
title: 'Android'
}
]
}
]
},
{
title: 'Printers'
}
];
使用例子,注意子级变量名称一致
在指令内,可以使用这样的结构
<div>
<ul>
<li ng-repeat="category in categories">{{ category.title }}</li>
</ul>
<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>
</div>
vi设计http://www.maiqicn.com 办公资源网站大全https://www.wode007.com
可以使用 ng-init 重命名子级变量名称
<li ng-repeat="parentCategory in categories"
ng-include="'categoryTree'"
ng-init="category=parentCategory">
</li>