你如何在Angularjs中进行递归层次计算?

时间:2015-10-03 23:04:13

标签: javascript angularjs hierarchy hierarchical-data

我需要动态计算可编辑树/层次结构中的后代总数。我知道如何观看物品而不是物品。

例如:

Item 1 | Number of Descendants: 5
  Item 1:1 | Number of Descendants: 3
    Item 1:1:1 | Number of Descendants: 0
    Item 1:1:2 | Number of Descendants: 0
    Item 1:1:3 | Number of Descendants: 0
  Item 1:2 | Number of Descendants: 0

我是否必须在所有级别上观看?

到目前为止我所拥有的:

var app = angular.module('app', []);

app.controller('exampleCtrl', function($scope) {

  $scope.item = {
    'name': 'Item 1',
    'numberOfDescendants': 5,
    'items': [{
      'name': 'Item 1:1',
      'numberOfDescendants': 3,
      'items': [{
        'name': 'Item 1:1:1',
        'numberOfDescendants': 0,
      }, {
        'name': 'Item 1:1:2',
        'numberOfDescendants': 0,
      }, {
        'name': 'Item 1:1:3',
        'numberOfDescendants': 0,
      }, ]
    }, {
      'name': 'Item 1:2',
      'numberOfDescendants': 0,
    }, ]
  };


});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app='app' ng-controller="exampleCtrl">
  
  <script type="text/ng-template" id="itemView">
    {{item.name}} |
    Number of Descendants: {{item.numberOfDescendants}}
    <ul>
      <li ng-repeat="item in item.items" ng-include="'itemView'"></li>
    </ul>
  </script>
  
  <div ng-include="'itemView'"></div>

</body>

0 个答案:

没有答案