我做了一些调查,似乎找不到最佳的模式来递归使用
ng-repeat
创建深层次的table
。用户mgdelmonte在这里提到了一些值得注意的内容,但遗憾的是没有解决方案。
HTML和模板:
<body ng-app="myApp">
<div ng-controller="myAppController">
<script type="text/ng-template" id="tree_item.html"></script>
<table class="table table-striped">
<thead>
<tr>
<th style="width:30px;">Test</th>
</tr>
</thead>
<tbody ng-repeat="data in treeData" ng-include="'tree_item.html'">
</tbody>
</table>
</div>
</body>
JS代码:
angular.module('myApp', []);
function myAppController($scope) {
$scope.treeData = [{
"name": "Root",
"nodes": [{
"name": "Level 1",
"nodes": [{
"name": "Level 2"
}]
}]
}];
}
这是我的jsfiddle:http://jsfiddle.net/8f3rL/86/
我已经实现了树形遍历,但当递归到下层时会丢失<tr>
标签,转而呈现为<span>
。
我猜测,当ng-repeat
运行在那个<div>
上时,它为那些tr
和td
创建了一个单独的作用域,这是不合法的(因为必须绑定到table
元素)。
将div
更改为table
或tbody
或tr
也不起作用(即使我也不想这样嵌套表)。
<tbody>
元素进行ng-repeat。这合法吗?我期望一个<table>
只有一个<tbody>
,但也许我很傻 :) 另一个愚蠢的问题,什么是“深表”? - Sunil D.