[英]AngularJS iterating through a multi-layered array with ng-repeat
I have an issue with the ng-repeat directive when it comes to iterating through an array with multiple layers. 在遍历具有多个层的数组时,ng-repeat指令存在问题。 Much like a Json object but not quite. 很像Json对象,但不完全是。
To build the array I use code akin to this: 要构建数组,我使用类似于以下代码:
$scope.dailyData = []
$scope.dailyData[0] = []
$scope.dailyData[0].push([{"today":null,"day2":1,"day3":null,"day4":null,"day5":null,"day6":null,"day7":null,"title":"Queries Built","id":null}])
This is just a sample of what the program actually does, and I do it this way for a specific reason that may or may not be relevant to the question. 这只是该程序实际功能的一个示例,出于某些可能与该问题无关的特定原因,我以这种方式进行操作。
This outputs to an array that looks like this. 这输出到看起来像这样的数组。
[[[{"title":"Queries Built","today":null,"day2":1,"day3":null,"day4":null,"id":null}]]]
There are three arrays and an object in the middle. 中间有三个数组和一个对象。 The problem is I can't seem to get the ng-repeat to pull the data from the object. 问题是我似乎无法获得ng-repeat来从对象中提取数据。
I want it to output like this: 我希望它输出如下:
| Title | Today | Yesterday |
| Queries Built | null | 1 |
But everything I've tried on ng-repeat doesn't seem to work. 但是我在ng-repeat上尝试过的所有方法似乎都不起作用。 One such example: 这样的一个例子:
<div ng-repeat="row in dailyData[0]">
<tr>
<td>{{row[0]["title"]}}</td>
<td>{{row["today"]}}</td>
<td>{{row["day2"]}}</td>
<td>{{row["day3"]}}</td>
<td>{{row["day4"]}}</td>
</tr>
</div>
The first row in here is a different method from the others, neither of them works. 这里的第一行是与其他行不同的方法,它们都不起作用。
Here's a fiddle: https://jsfiddle.net/rms9dv8j/2/ 这是一个小提琴: https : //jsfiddle.net/rms9dv8j/2/
I think you need to restructure your data so that it is in a better format. 我认为您需要重组数据,以便采用更好的格式。
You will need to do a nested loop similar to what I've done in this plnkr: http://plnkr.co/edit/CqUHwj?p=info 您将需要执行类似于我在plnkr中所做的嵌套循环: http ://plnkr.co/edit/CqUHwj?p=info
<div ng-app="module" ng-controller="ctrl">
<div ng-repeat="row in array">
{{row.name}}
<div ng-repeat="row2 in row.array2">
{{row2.name}}
</div>
</div>
</div>
More helpful information can be found in this post: 在这篇文章中可以找到更多有用的信息:
The main problem is that you used a <DIV>
tag in the middle of the table, which is probably why the browser didn't render anything. 主要问题是您在表的中间使用了<DIV>
标记,这可能是浏览器未呈现任何内容的原因。
In this fiddle: 在这个小提琴中:
https://jsfiddle.net/5ox7Ledw/ https://jsfiddle.net/5ox7Ledw/
<div ng-repeat="row in dailyData[0][0]">
<tr>
<td>{{row[0]["title"]}}</td>
I simplified the data and moved the ng-repeat into the TR element, while also deleting the erroneous DIV 我简化了数据并将ng-repeat移到TR元素中,同时还删除了错误的DIV
<tr ng-repeat="row in dailyData">
<td>{{row.title}}</td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.