Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

angularjs

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"
        }]
    }]
}]
like image 850
Hairgami_Master Avatar asked Mar 21 '13 18:03

Hairgami_Master


People also ask

What is a property name in JSON?

In JSON, the “keys” must always be strings. Each of these pairs is conventionally referred to as a “property”.

What is JSON in Angular JS?

AngularJS json FilterThe json filter converts a JavaScript object into a JSON string. This filter can be useful when debugging your applications. The JavaScript object can be any kind of JavaScript object.


1 Answers

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.

like image 196
Rajkamal Subramanian Avatar answered Sep 28 '22 10:09

Rajkamal Subramanian