文档:https://code.angularjs.org/1.2.26/docs/api/ng/directive/ngRepeat
The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index is set to the item index or key.
看起来我得到的是索引而不是键。
<小时/>Controller :
$scope.items = {
"key1" : "val1",
"key2" : "val2"
};
查看:
<div class="item" ng-repeat="item in items">{{ $index }} {{item}}</div>
结果:
0 val1
1 val2
http://plnkr.co/edit/0g9EL6kqYcm4jNpdDZ9L?p=preview
我想显示键,在本例中应该是 key1
和key2
。有特殊的语法吗?
最佳答案
尝试
<div class="item" ng-repeat="(key, value) in items">{{ $index }} {{key}} {{value}}</div>
关于javascript - AngularJS ng-repeat 如何获取 item key?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26440662/