简体   繁体   English

AngularJS ng-重复未知的深度数组

[英]AngularJS ng-repeat an unknown deep array

I need to have the possibility to print out a possible infinite deep array-structure into a list. 我需要将可能的无限深数组结构打印到列表中。 Here's the code from a modified AngularJS-example where I have added some more deep children in the array. 这是经过修改的AngularJS示例的代码,在该示例中,我在数组中添加了一些更深的子级。 How can I print child of child and even its children as well? 如何打印child of child ,甚至它的孩子? Is there a way I can get it to repeat forever as long as there is more children? 只要有更多的孩子,有没有办法让我永远重复下去?

HTML 的HTML

<div ng-app>
  <div ng-controller="TodoCtrl">
    <ul>
      <li ng-repeat="todo in todos">
        <span>{{todo.text}}</span>
        <ul>
          <li ng-repeat="child in todo.children">
            <span>{{child.text}}</span>
            <!-- What about the next one -->
          </li>
        </ul>
      </li>
    </ul>
  </div>
</div>

JS JS

function TodoCtrl($scope) {
  $scope.todos = [{
    text: 'root 1',
    children: [{
      text: 'child of root 1',
      children: [{
        text: 'child of child',
        children: []
      }]
    }]
  }, {
    text: 'root 2',
    children: []
  }];
}

Fiddle: https://jsfiddle.net/U3pVM/25866/ 小提琴: https : //jsfiddle.net/U3pVM/25866/

You can use the view recursively this way: 您可以通过这种方式递归使用视图:

<script type="text/ng-template" id="todo.html">
    <div>
        <div>{{ todo.text }}</div>
        <ul>
            <li ng-repeat="todo in todo.children" ng-include="'todo.html'"></li>
        </ul>
    </div>
</script>

<div ng-controller="MyController">
  <ul>
    <li ng-repeat="todo in todos" ng-include="'todo.html'"></li>
  </ul>
</div>

Here the DEMO based on your sample data. 这里是基于您的样本数据的DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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