2

I have a cross platform app developed in AngularJS, Onsen UI and Monaca.

On one of my pages I make a API call that returns a nested JSON object to me. I can read the JSON object using $http.get() and I can loop through the JSON and display the high level items, but I cant seem to display the nested items.

What I want to do is display the high level items as a text field and then display the nested items as radio button for each of the high level items.

An example of my JSON file would look something like this:

[{
    "itemID": "1",
    "itemDescription": "Apple",
    "options": [{
        "fruitCheckID": "1",
        "fruitDescription": "Ok"
    }, {
        "fruitCheckID": "2",
        "fruitDescription": "Not Ripe"
    }, {
        "fruitCheckID": "3",
        "fruitDescription": "Rotten"
    }]
}, {
    "itemID": "2",
    "itemDescription": "Orange",
    "options": [{
        "fruitCheckID": "1",
        "fruitDescription": "Ok"
    }, {
        "fruitCheckID": "2",
        "fruitDescription": "Not Ripe"
    }, {
        "fruitCheckID": "3",
        "fruitDescription": "Rotten"
    }]
}]

And in my fruit.html page I want to display the the fruit names (itemDescription) as a text fields with radio buttons below them for each of the "fruitDescription" items.

My fruit.html page looks as follows:

<ul class="list">
    <li class="list__item" ng-repeat="checkItemDescription in data">
        <span class="list__item__line-height"><strong>{{checkItemDescription.itemDescription}}</strong></span>  <!-- WORKING HERE -->

        <label class="checkbox">
            <input type="checkbox"
                ng-click="toggleSelection(checkItemDescription.itemDescription)">
            <div class="checkbox__checkmark"></div>
            {{checkItemDescription.options}}  <!-- NOT WORKING HERE -->
        </label>
    </li>
</ul>

What I get when I run the above displays the fruit names in a list as I want, but for each fruit item there is only 1 radio button displayed and then the JSON formatted code e.g.

  • Apple
  • (Only 1 radio button displayed here)
  • [{"fruitCheckID": "1", "fruitDescription": "Ok" }, { "fruitCheckID": "2", "fruitDescription": "Not Ripe" }, { "fruitCheckID": "3", "fruitDescription": "Rotten" }]

Where the 3rd bullet point in that list is actually how the items are displayed to me.

In my app.js I get the JSON object as below and when I console.log() the JSON object its all displaying as it should.

// Loop through the JSON [object Object]...[object Object] returned from API call
angular.forEach($scope.data, function(value, key)
{   
    // WORKING
    console.log("Item ID: " + value.itemID);
    console.log("Item Description: " + value.itemDescription);

    // Inner loop to return nested JSON objects
    angular.forEach(value.options, function(v, k)
    {
        // WORKING
        console.log("Fruit Check ID: " + v.fruitCheckID);
        console.log("Fruit Description: " + v.fruitDescription);
    });
});

Can anyone tell me where I am going wrong with the format of displaying the nested values?

2
  • 2
    Your question has its answer too. as using 2 foreach to display all items , you need 2nd ng-repeat on options to display radio buttons. Commented May 5, 2016 at 8:17
  • try this: {{angular.toJson(options.fruitDescription)}} Commented May 5, 2016 at 13:47

1 Answer 1

0

Try using this code..

 <ul class="list">
<li class="list__item" ng-repeat="checkItemDescription in data">
    <span class="list__item__line-height"><strong>   {{checkItemDescription.itemDescription}}</strong></span>  <!-- WORKING HERE -->

    <label class="checkbox">
        <input type="checkbox"
            ng-click="toggleSelection(checkItemDescription.itemDescription)">
        <div class="checkbox__checkmark" ng-repeat="options in data.options "></div><!-- use ng-repeat here -->
        {{options.fruitDescription}}  
    </label>
</li>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.