0

I'm working on a project in AngularJS.

I have JSON as

{
  "leagues":{
    "aLeague":{
      "country":"aCountry",
      "matchs":{
        "aUniqueID1":{
          "date":"2014-09-07 13:00:00",
          "guest_player":"Me",
          "host_player":"Tom",
          "odds":{
            "guest":"2.80",
            "host":"2.25",
            "null":"2.85"
          },
          "score":"0 - 0"
        },
        "aUniqueID2":{
          "date":"2014-09-07 18:30:00",
          "guest_player":"Bryan",
          "host_player":"Me",
          "odds":{
            "guest":"3.25",
            "host":"1.98",
            "null":"2.95"
          },
          "score":"0 - 0"
        }
      }
    }
  }
}

And I want to display it guest_playerand host_playerin a ng-repeat.

But I'm not able to access to aUniqueID1 or aUniqueID2.

Any ideas?

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
user1269586
  • 372
  • 1
  • 6
  • 27

2 Answers2

2

Please see here:http://jsbin.com/wekow/1/edit

<p ng-repeat="(key, item) in data.leagues.aLeague.matchs">
Guest Player :{{item.guest_player}}<br/>
Host Player :{{item.host_player}}
</p>
sylwester
  • 16,498
  • 1
  • 25
  • 33
  • Yeah thanks it's working :-) last things, do you know how can I check if `matchs` is empty ? – user1269586 Sep 03 '14 at 15:50
  • @user1269586 when do you want to check that ? – sylwester Sep 03 '14 at 15:52
  • @user1269586 please see here http://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – sylwester Sep 03 '14 at 15:58
  • @sss @Josep Here : `
    • {{item.host_player}} vs {{item.guest_player}}
    ` . (`lesMatchs` = `data.leagues`)
    – user1269586 Sep 03 '14 at 16:01
  • @user1269586 Object.keys(data.leagues.aLeague.matchs).length?"not empty":"emptpy". Object.keys(data.leagues.aLeague.matchs) will retrieve an array with the keys of "data.leagues.aLeague.matchs" , therefore in your case Object.keys(data.leagues.aLeague.matchs).length will retrieve the number of matches. – Josep Sep 03 '14 at 16:20
1

Like this?:

<li ng-repeat="(key, item) in data.leagues.aLeague.matchs">
<span>User ID: {{key}}</span>
<span>Guest Player: {{item.guest_player}}</span>
</li>

The first span will display the key of the item (the value that you were asking about), the second one the attribute guest_player of the item.

Josep
  • 12,926
  • 2
  • 42
  • 45