我有一个包含键值对的对象,如下所示。您将看到该键具有一个数组作为其值。
$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>
最佳答案
只需使用另一个 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。
关于javascript - ng-repeat through object with value 作为数组 Angular ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38832240/