javascript - AngularJS ng-repeat 如何获取 item key?

标签 javascript angularjs angularjs-ng-repeat

文档: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

我想显示,在本例中应该是 key1key2 。有特殊的语法吗?

最佳答案

尝试

<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/

相关文章:

javascript - Angular-js : ng-repeat on a select element with a attribute directive

javascript - onkeypress ="return isNumberKey(event, this);"在 IE8 中调用该函数两次,每次在 javascript 中按下

javascript - 如何获取多个选择框值并插入字符串数组

javascript - Angular js Controller 不工作

javascript - 在angularjs转发器内的输入日期将字符串转换为日期

javascript - 使用单选按钮重复 ng 时出现错误

javascript - 无限滚动php发帖重复

javascript - 在 WebStorm IDE 中突出显示自定义语法结构

javascript - Mysql AES_ENCRYPT => 客户端 JavaScript Aes.Ctr.decrypt 问题

javascript - 如何在 Angular-UI-Router 中的所有命名 View 中更新作用域变量