【问题标题】:How can I access a JSON property name in AngularJS if I don't know what it is?如果我不知道它是什么,如何在 AngularJS 中访问 JSON 属性名称?
【发布时间】:2013-03-11 11:01:42
【问题描述】:

给定 JSON 对象 (formData),我尝试使用 AngularJS 遍历该对象并输出 RealEstateInfoPersonalTaxInfo。对于我的生活,我似乎无法弄清楚如何获得属性名称。有什么想法吗?

顺便说一下,(key,value) 不起作用。 key 给我索引号,value 整个对象。

<ul>
    <li ng-repeat="item in formsData">
        {{item.value}} //What goes here to get "RealEstateInfo" the 1st loop, and "PersonalTaxInfo" the second loop?
    </li>
<ul>

$scope.formData = [
{
    "RealEstateInfo": [
    {
        "Group": "General",
        "Fields": [
        {
            "Name": "TitleType",
            "Label": "Title Type",
            "Type": "dropdown",
        },
        {
            "Name": "NameIfAvailable",
            "Label": "Name if available",
            "Type": "string"
        }]
    },
    {
         "Group": "Personal",
         "Fields": [
         {
             "Name": "TitleType",
             "Label": "Title Type",
             "Type": "dropdown",
         },
         {
             "Name": "NameIfAvailable",
             "Label": "Name if available",
             "Type": "string"
         }]
     }]
},
{
    "PersonalTaxInfo": [
    {
        "Group": "General",
        "Fields": [
        {
             "Name": "TitleType",
             "Label": "Title Type",
             "Type": "dropdown",
        },
        {
            "Name": "NameIfAvailable",
            "Label": "Name if available",
            "Type": "string"
        }]
    },
    {
        "Group": "PersonalInfo",
        "Fields": [
        {
             "Name": "TitleType",
             "Label": "Title Type",
             "Type": "dropdown",
        },
        {
             "Name": "NameIfAvailable",
             "Label": "Name if available",
             "Type": "string"
        }]
    }]
}]

【问题讨论】:

    标签: json angularjs


    【解决方案1】:

    请看看这个小提琴。 http://jsfiddle.net/4UTHW/

    ng-repeat="(key,value) in data" 
    

    使用此语法会将对象的键分配给key 变量,并将这些键的值分配给value 变量。

    为简洁起见简化了 json 结构。

    【讨论】:

    • @rajkamal- 非常感谢!这很有效,尽管我并不完全理解嵌套的 ng-repeat 概念。我认为,它与标准编码实践中每个循环的嵌套相同......
    • @rajkamal 检索属性名称是新的,虽然这真的很有帮助!
    猜你喜欢
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    相关资源
    最近更新 更多