【发布时间】:2016-09-30 15:48:51
【问题描述】:
我正在尝试解决 ng-repeat 问题。我可以看到 2 种方法来解决它 1. 使用 filter 不重复/显示 category 的对象属性,如果不使用它的值或 2,我不知道该怎么做。如果有办法说ng-repeat="i in items.drinks && items.sandwiches.
没有通配符可以说“类别”:“随便”吗?
所以在这种情况下,我将深入到 item = bottled-drinks 和/或 sandwiches 的位置。所以当我做ng-repeat="i in item" category 时,我不想要它。
json -
food-items": {
"bottled-drinks": {
"category": "Bottled Drinks",
"drinks": {
"drink1": {
"title": "Drink Me 1",
"calories": "170"
},
"drink2": {
"title": "Drink Me 2",
"calories": "230"
},
"drink3": {
"title": "Drink Me 3",
"calories": "88"
}
}
},
"sandwiches": {
"category": "Sandwiches",
"sandwiches": {
"sandwich1": {
"title": "Sandwich Me 1",
"calories": "230"
},
"sandwich2": {
"title": "Sandwich Me 2",
"calories": "111"
},
"sandwich3": {
"title": "Sandwich Me 3",
"calories": "700"
}
}
}
【问题讨论】:
-
你控制那个数据结构源吗?这是一个非常不友好的结构
-
@charlietfl 不,但是您将如何以不同的方式构建它?
-
具有一致属性名称的对象数组会更容易使用,并且为了进行过滤或排序,您需要映射到该数组
-
@charlietfl 你能给我一个例子,我会建议它。
标签: javascript angularjs json angularjs-ng-repeat ng-repeat