嵌套的ng-repeat angularjs

时间:2016-05-17 06:57:25

标签: javascript arrays angularjs

我有一个类似于我的问题是我不知道有多少嵌套那个虚拟数组对象所以如何使用ng-repeat打印所有虚拟数组对象

demo: [{
    id: 1,
    dummy: [
        {
            id: 1,
            dummy: [
                {
                    id: 1,
                    dummy: [
                        {
                            id: 1
                        }
                    ]
                }
            ] 
        }
    ]
}]

1 个答案:

答案 0 :(得分:2)

试试这样。这样回答:How to put ng-repeat inside ng-repeat for n number of timesHow can I make recursive templates in AngularJS when using nested objects?

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

app.controller('mainCtrl', function($scope){
  $scope.demo = [{
    id: 1,
    dummy: [
        {
            id: 1,
            dummy: [
                {
                    id: 1,
                    dummy: [
                        {
                            id: 1,
                             dummy: [
                        {
                            id: 1,
                             dummy:[]
                        }
                       ]
                        }
                    ]
                }
            ] 
        }
    ]
}]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet"/>

<div ng-app="app" ng-controller="mainCtrl">
  <script type="text/ng-template" id="dummy.html">
    <ul>
      <li ng-repeat="d in demo" >
        <div >{{d.id}}</div>
        <div ng-show="d.dummy.length"  ng-include=" 'dummy.html' " onload="demo = d.dummy"></div>
      </li>
    </ul>
  </script>
  <div ng-include=" 'dummy.html'" ></div>
</div>