在对象数组上重复 ng-repeat 与包含对象的对象 [英] ng-repeat over array of objects vs. object containing objects
问题描述
在我见过的几乎所有 ng-repeat 示例中,数据的结构都是这样的:
$scope.dataCollected = [{name: bob, data: '10-12-12'}, {name:joe, data: '09-13-13'}];
然而,让 ng-repeat 工作的唯一方法是,如果我这样构造数据:
$scope.dataCollected = {bob: {name: bob, data: '10-12-12'}, joe: {name:joe, data: '09-13-13'}};
将其构造为数组会导致 ng-repeat 完全不做任何事情.它甚至不会给出错误.将它构建为包含对象的对象是可行的,但我想使用数组,因为我知道这是在 ng-repeat 上使用过滤器的唯一方法.
我这样调用 ng-repeat:
<h3 class="name">{{data.name}} </h3>
我错过了什么?
抱歉各位,感谢您的帮助.问题是,为了使我的数据更易于阅读,我使用括号表示法为数组的键分配了名称,如此处的答案所示:stackoverflow.com/questions/12244483/... ng-repeat 根本不喜欢那样.似乎需要默认密钥.
In almost all the examples of ng-repeat I've seen, the data is structured like so:
$scope.dataCollected = [{name: bob, data: '10-12-12'}, {name:joe, data: '09-13-13'}];
However, the only way I can get ng-repeat to work is if I structure the data like this:
$scope.dataCollected = {bob: {name: bob, data: '10-12-12'}, joe: {name:joe, data: '09-13-13'}};
Structuring it as an array causes the ng-repeat to do absolutely nothing. It doesn't even give an error. Structuring it as an object containing objects works, but I'd like to use an array because I understand it's the only way to use a filter on ng-repeat.
I'm calling the ng-repeat like this:
<div class="list-row" ng-repeat="data in dataCollected">
<h3 class="name"> {{data.name}} </h3>
</div>
What am I missing?
Sorry guys, thank you for the help. The issue was that in an attempt to make my data easier to read, I had assigned names to the keys of the array using bracket notation as seen in the answer here: stackoverflow.com/questions/12244483/… ng-repeat does not like that at all. It seems the default keys are necessary.
这篇关于在对象数组上重复 ng-repeat 与包含对象的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!