AngularJS:如何获得一个JSON对象的关键 [英] AngularJS: How to get the key of a JSON Object

查看:240
本文介绍了AngularJS:如何获得一个JSON对象的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能确定这是否已经得到了什么与所有AngularJS,如果它仅仅是JSON有关。

总之,让我们说,我们有以下JSON:

  $ scope.dataSets = {
    名字:贺,斯拉格霍恩,西弗勒斯,斯内普],
    性别:男,女]
}

现在,我使用的是NG-重复指令打印上面如下:

 < D​​IV NG重复=数据集>
    //继续readig知道我在这里expcting
< / DIV>

我有什么期望的&LT中; DIV>< / DIV> 标签是打印名和性别。也就是说,我要打印的JSON的钥匙。我不知道键是什么,在他们可以是任何东西。我怎样才能做到这一点?


解决方案

作为文档说明吧:


  

(键,值)前pression - 其中key和value可以是任何用户定义的标识符,和前pression是范围前pression给集合枚举<。 / EM>


 &LT; D​​IV NG重复=(键,数据)的数据集&GT;
  {{键}}
&LT; / DIV&GT;

I am unsure if this has got anything to do with AngularJS at all and if it is only JSON related.

Anyhow, let us say that we have the following JSON:

$scope.dataSets = {
    "names": ["Horace", "Slughorn", "Severus", "Snape"],
    "genders": ["Male", "Female"]
}

Now, I am using the ng-repeat directive to print the above as follows:

<div ng-repeat="data in dataSets>
    //Continue readig to know what I am expcting here
</div>

What I expect within the <div></div> tags is to print "name" and "genders". That is, I wish to print the keys of the JSON. I have no idea what the keys are, as in they could be anything. How can I do this?

解决方案

As docs state it:

(key, value) in expression – where key and value can be any user defined identifiers, and expression is the scope expression giving the collection to enumerate.

<div ng-repeat="(key, data) in dataSets">
  {{key}}
</div>

这篇关于AngularJS:如何获得一个JSON对象的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆