Hairgami_Master
Hairgami_Master

Reputation: 5539

How can I access a JSON property name in AngularJS if I don't know what it is?

Given the JSON object (formData), I'm trying to loop through the object with AngularJS and output RealEstateInfo and PersonalTaxInfo. For the life of me, I can't seem to figure out how to get at the property name. Any ideas?

By the way, (key,value) does not work. key gives me the index number, value the entire object.

<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"
        }]
    }]
}]

Upvotes: 5

Views: 10007

Answers (1)

Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6944

Please have a look at this fiddle. http://jsfiddle.net/4UTHW/

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

using this syntax will assign the keys of an object to key variable and values of those keys to value variable.

Simplified the json structure for brevity.

Upvotes: 5

Related Questions