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>可以使用 ng-init 重命名子级变量名称
<li ng-repeat="parentCategory in categories"
ng-include="'categoryTree'"
ng-init="category=parentCategory">
</li>
本文内容仅供个人学习/研究/参考使用,不构成任何决策建议或专业指导。分享/转载时请标明原文来源,同时请勿将内容用于商业售卖、虚假宣传等非学习用途哦~感谢您的理解与支持!