我有一个带有键值对的对象,看起来像这样。您将看到该键具有一个数组作为其值。
$scope.testObj = {
"London":[
{"id":1,"city":"London","country":"GB","name":"Test1"},
{"id":4,"city":"London","country":"GB","name":"Test2"}
],
"Los Angeles":[
{"id":8,"city":"LA","country":"US","name":"Test3"}
]
}
我想使用angular显示前端城市旁边的名称。要做到这一点,我尝试了很多方法,并使用$ index跟踪,但无法弄清楚如何使其工作。
<div ng-repeat="(key, val) in jobsByCity track by $index">
{{key}}:{{val[$index].name}}
</div>
我也看过这种方法,嵌套ng-repeat
<div ng-repeat="(key, val) in testCity">
{{key}}
<div ng-repeat="test in val[$index].name">
{{test}}
</div>
</div>
答案 0 :(得分:3)
只需使用其他ng-repeat
来迭代值:
<div ng-repeat="(key, val) in jobsByCity">
<div ng-repeat="subValue in val track by $index">
{{key}}:{{subValue.name}}
</div>
</div>
另请注意,您的Los Angeles
属性需要在引号中,否则它不是有效的javascript。