简体   繁体   English

嵌套ng-repeat的深度— AngularJS

[英]Depth of nested ng-repeat — AngularJS

Is there a way to get the depth of a nesting ng-repeats? 有没有办法获得嵌套ng-repeats的深度? Say if you are iterating over an nested object of unknown depth like nested comments? 假设您要遍历嵌套深度未知的嵌套对象(例如嵌套注释)?

Since ng-repeat creates a new scope, you could do something like this. 由于ng-repeat创建了新作用域,因此您可以执行以下操作。

{{$depth = $parent.$depth && $parent.$depth + 1 || 1}}

See https://jsfiddle.net/crgt25uk/ for a working example. 有关工作示例,请参见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'}]}
    ]
})

And your template: 和您的模板:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM