在ng-repeat中获取对象键

时间:2014-03-21 00:57:35

标签: javascript angularjs object

如果我有如下对象:

languages = {
    "ar":{
        "name":"Arabic",
        "nativeName":"العربية"
    },
    "bg":{
        "name":"Bulgarian",
        "nativeName":"български език"
    },
    "ca":{
        "name":"Catalan; Valencian",
        "nativeName":"Català"
    }...
}

我在这样的列表中循环它:

 <ul>
      <li ng-repeat="lang in languages"><a ng-click="select(lang)">{{lang.nativeName}}</a></li>
 </ul>

有没有办法在select函数中获取对象键而不将键放在对象本身中?

即:

languages = {
        "ar":{
            "name":"Arabic",
            "nativeName":"العربية",
            "key":"ar"
        },

感谢。

1 个答案:

答案 0 :(得分:2)

你可以这样做:

 <ul>
      <li ng-repeat="(key, lang) in languages"><a ng-click="select(key)">{{lang.nativeName}}</a></li>
 </ul>