我试图压扁嵌套的json对象&将它显示在表格中。
所以我尝试使用多个ng-repeat-start& div中的ng-repeat-end元素。 但不知何故,数据没有显示出来。
普兰克: http://plnkr.co/edit/apuf9YUYnkoSaCDaNYrC?p=preview
HTML:
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.2.x" src="https://code.angularjs.org/1.2.22/angular.js" data-semver="1.2.22"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
Data is :
{{mydata}}
<table cellspacing="1" border="1">
<thead>
<tr>
<td> MainTitle</td>
<td> SubTitle</td>
<td> LastTitle</td>
<td> Final</td>
</tr>
</thead>
<tbody>
<div ng-repeat-start="main in mydata" ng-if="false"></div>
<div ng-repeat-start="sub in main.items" ng-if="false"></div>
<div ng-repeat-start="last in sub.l2items" ng-if="false"></div>
<tr ng-repeat="final in last.l3items">
<td> {{main.title}} </td>
<td> {{sub.title}} </td>
<td> {{last.title}} </td>
<td> {{final}} </td>
</tr>
</tbody>
<div ng-repeat-end ng-if="false"></div>
<div ng-repeat-end ng-if="false"></div>
<div ng-repeat-end ng-if="false"></div>
</table>
</body>
</html>
代码:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.mydata = [
{
title :"hello",
items : [
{
title: "level 1 title",
l2items : [
{
title : "level 2 title",
l3items: [ "a","b" ]
}
]
}
,
{
title: "level 1 title 1 ",
l2items : [
{
title : "level 2 title 1",
l3items: [ "a 1","b 1" ]
}
]
}
]
},{
title :"hello 0",
items : [
{
title: "level 1 title 0",
l2items : [
{
title : "level 2 title 0",
l3items: [ "a 0","b 0" ]
}
]
}
,
{
title: "level 1 title 1 0",
l2items : [
{
title : "level 2 title 1 0",
l3items: [ "a 1 0","b 1 0" ]
}
]
}
],
}]
});
预期输出(道歉无法创建合适的表格)
主标题|副标题| LastTitle |最终
你好| 1级标题| 2级标题| a
你好| 1级标题| 2级标题| b
你好| 1级标题1 | 2级标题1 | a 1
你好| 1级标题1 | 2级标题1 | b 1
你好0 | 1级标题0 | 2级标题0 | a 0
你好0 | 1级标题0 | 2级标题0 | b 0
你好0 | 1级标题1 0 | 2级标题1 0 | a 1 0
你好0 | 1级标题1 0 | 2级标题1 0 b 1 0
答案 0 :(得分:3)
我认为问题出在您的HTML上。很可能在你的桌子里有div。如果您将其转换为<div>
s,那么它可以正常工作。我看到的另一个问题是您正在重复关闭</body>
代码。
我理解你为什么要使用<div>
和ng-if
来做这件事。曾经有ng-repeat
的评论指令版本,或至少是创建一个的评论指令版本,但我认为它们遇到了browser limitations。
这是similar issue。也许你会喜欢他们的解决方案。
否则,请将<div>
替换为<tr>
s:
<tr ng-repeat-start="main in mydata" ng-if="false"></tr>
<tr ng-repeat-start="sub in main.items" ng-if="false"></tr>
<tr ng-repeat-start="last in sub.l2items" ng-if="false"></tr>
<tr ng-repeat="final in last.l3items">
<td> {{main.title}} </td>
<td> {{sub.title}} </td>
<td> {{last.title}} </td>
<td> {{final}} </td>
</tr>
<tr ng-repeat-end ng-if="false"></tr>
<tr ng-repeat-end ng-if="false"></tr>
<tr ng-repeat-end ng-if="false"></tr>