【发布时间】:2014-09-14 05:26:56
【问题描述】:
我已经做了一些研究,但我似乎找不到递归使用ng-repeat 来创建深层table 的最佳模式。用户 mgdelmonte 有一些值得注意的提及 here 关于该主题,但很遗憾,没有解决方案。
HTML 和模板:
<body ng-app="myApp">
<div ng-controller="myAppController">
<script type="text/ng-template" id="tree_item.html">
<tr style="width:100%">
<td>{{data.name}}</td>
</tr>
<div ng-repeat="data in data.nodes" ng-include="'tree_item.html'">
</div>
</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>s。
我的直觉告诉我,当ng-repeat 在<div> 上运行时,它为trs 和tds 创建了一个单独的范围,我猜这是非法的(因为它们必须是绑定到table 元素)。
将div 更改为table 或tbody 或tr 也是无操作的(我什至不想像那样嵌套表格)。
【问题讨论】:
-
也许这是一个愚蠢的问题,但您正在使用
<tbody>元素进行 ng-repeat。那合法吗?我希望<table>恰好有一个<tbody>,但也许我很笨:) 另一个愚蠢的问题,什么是“深表”? -
一点也不笨。表格内的 tbody 和 div 是问题所在。
标签: angularjs ng-repeat angularjs-ng-include