我的情况与此问题的答案非常相似:
AngularJS ng-include with nested hierarchy
我有一些格式为
的数据 $scope.data = {
text: "blah",
comments:
[
{
text: ["blahL11", "blahL12", "blahL13"],
comments: [
{
text: ["blahL111", "blahL112", "blahL113"]
},
{
text: ["blahR111", "blahR112", "blahR113"]
}
]
},
{
text: ["blahR11", "blahR12", "blahR13"]
}
]
};
我用这样的递归ng-include显示它:
<ul>
<li>{{data.text}}</li>
<li ng-repeat="item in data.comments" ng-include="'tree'"></li>
</ul>
<script type="text/ng-template" id="tree">
<ul>
<li ng-repeat="text in item.text">{{text}}</li>
<li ng-repeat="item in item.comments" ng-include="'tree'"></li>
</ul>
</script>
http://plnkr.co/edit/8swLos2V6QRz6ct6GDGb?p=info
但是,我想以某种方式跟踪递归的深度。所以,而不是简单地显示:
-blah
-blahL11
-blahL12
-blahL13
-blahL111
可以显示
-1. blah
-2. blahL11
-2. blahL12
-2. blahL13
-3. blahL111
没有我修改数据结构(因为深度只是真正用于显示?)。我可以在ng-include中插入一个变量,是否可以使用某种递归$ index?
答案 0 :(得分:3)
您可以使用<li ng-repeat="item in item.comments" ng-include="'tree'" ng-init="depth = depth + 1"></li>
执行此操作。这将在创建新范围时为其分配一个值,您可以通过引用旧范围中的值并将其递增来实现。
Validation