Melanie Strathdee
Melanie Strathdee

Reputation: 3

Json Lists in arrays ionic return list values

I'm new to ionic and I want to be able to extend a simple json data set to include lists within an array: My json file looks like this:

angular.module('starter.services', [])

/** A simple example service that returns some data. */
.factory('Bands', function() {

  var bands = [
         {"id": "0", name: 'U2', nationality: 'Irish', category: 'Rock', pic: "U2.jpg", url:"www.u2.com" },                     
       {
            "albums":
                {"album"
                    [
                        { "id": "101", name:"Songs Of Innocence", year:"2014", pic: "u2_soi_cover.jpg" },
                        { "id": "102", name:"No Line On The Horizon", year:"2009", pic: "u2_nloth_cover.jpg" },
                        { "id": "103", name:"How To Dismantle An Atomic Bomb", year:"2004", pic: "u2_htdaab_cover.jpg" },
                     ]
                },
      },
   {"id": "1", name: 'Silverchair', nationality: 'Australian', category: 'Grunge', pic: "silverchair.jpg", url:"www.silverchair.com/" },                        
       {
            "albums":
                {"album"
                    [
                        { "id": "102", name:"Frogstomp", year:"1995", pic: "sc_frogstomp_cover.jpg" },
                    ]
                },

},
 ];

return {
all: function() {
  return bandss;
},
get: function(bandId) {
  // Simple index lookup
  return bands[bandId];
  }
 }
 })

So I have been able to return the list of bands using a repeat and pass the band id to display individual band details.

I want to no extend the band page to it to return the album list details so I'm guessing it would be something like this, but I need some help understanding how to get the list out of the array for a specific band id.

 <ion-content>
      <div class="details">
      <img src="pics/bands/{{band.pic }}" />
    <h2>{{band.name}}</h2>
   <p>{{band.nationality}}</p>
   </div>
  <ion-list>
      <ion-item ng-repeat="album in albums" type="item-text-wrap" >
             <h2>{{album.name}}</h2>
      <p>{{album.year}}</p>
        </ion-item>
    </ion-list>
   </ion-content>

Any help to point me in the right direction would be great.

Upvotes: 0

Views: 6265

Answers (1)

T4deu
T4deu

Reputation: 1135

You need to change the json format, move the "albums" into the "bands", like this:

[
  {
    "id": "0",
    "name": "U2",
    "nationality": "Irish",
    "category": "Rock",
    "pic": "U2.jpg",
    "url": "www.u2.com",
    "albums": [
        {
            "id"": "101",
            "name": "SongsOfInnocence",
            "year": "2014",
            "pic": "u2_soi_cover.jpg"
        }
    ]
  }
]

Now in the ng-repeat of your view:

<ion-item ng-repeat="album in band.albums" type="item-text-wrap" >
  <h2>{{album.name}}</h2>
  <p>{{album.year}}</p>
</ion-item>

Upvotes: 1

Related Questions