[英]Depth of nested ng-repeat — AngularJS
有沒有辦法獲得嵌套ng-repeats的深度? 假設您要遍歷嵌套深度未知的嵌套對象(例如嵌套注釋)?
由於ng-repeat
創建了新作用域,因此您可以執行以下操作。
{{$depth = $parent.$depth && $parent.$depth + 1 || 1}}
有關工作示例,請參見https://jsfiddle.net/crgt25uk/ 。
var app = angular.module('app',[]);
app.controller('ExampleController',function($scope){
$scope.items = [
{name:'foo',items:[{name:'foofoo'},{name:'foobar'}]},
{name:'bar',items:[{name:'barfoo'},{name:'barbar'}]}
]
})
和您的模板:
<div ng-app="app">
<div ng-controller="ExampleController">
<li ng-repeat="item in items">{{$depth = $parent.$depth && $parent.$depth + 1 || 1}}: {{item.name}}
<ul>
<li ng-repeat="item in item.items">{{$depth = $parent.$depth && $parent.$depth + 1 || 1}}: {{item.name}}</li>
</ul>
</li>
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.