【问题标题】:Nested ng-repeat angularjs嵌套的 ng-repeat angularjs
【发布时间】:2016-09-13 03:24:43
【问题描述】:

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

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

【问题讨论】:

  • 看到这个不错的答案。 stackoverflow.com/questions/37108946/…
  • 它只处理两个级别
  • 不确定,但您可以创建一个指令,该指令将打印当前元素并使用新的虚拟值调用自身。

标签: javascript arrays angularjs


【解决方案1】:

试试这样。正如这个答案: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>

【讨论】:

  • @Walfrat 但我在 chrome 上测试了这个!
  • 好吧,我完全不知道为什么,现在它正在显示......我删除了旧评论说“不工作”,所以人们不会停止。
  • 谢谢你现在工作正常,我接受你的回答,但我有一个问题如果我需要按降序显示它意味着最后一个虚拟数组对象首先显示。有可能吗?
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多