I've spent ages trying to solve this, and I can't...
- I have an array:
clubData. - In that array is a series of objects, which represent individual football clubs.
- In each club object, is an array assigned to the property
squad.
Here's a sample of clubData:
[{
"league": "0",
"id": "0",
"name": "AFC Bournemouth",
"codeName": "afc-bournemouth",
"nickName": "Cherries",
"dateFounded": "1899",
"stadium": "Dean Court",
"manager": "Eddie Howe",
"squad":
[
{"clubCode": "afc-bournemouth",
"firstName": "Artur",
"lastName": "Boruc",
"nationality": "pl",
"country": "Poland",
"position": "gk",
"shirtNumber": "1",
"age": "36"},
{"clubCode": "afc-bournemouth",
"firstName": "Glenn",
"lastName": "Murray",
"nationality": "gb-eng",
"country": "England",
"position": "fw",
"shirtNumber": "27",
"age": "32"}
]
}, {
"league": "0",
"id": "1",
"name": "Arsenal",
"codeName": "arsenal",
"nickName": "Gunners",
"dateFounded": "1886",
"stadium": "Emirates Stadium",
"manager": "Arsène Wenger",
"squad": [ etc... ]
}
]
I want to run an ng-repeat on an md-card (Angular Material directive), so every object (player) in every squad is repeated as a card.
My current method:
I used an outer and inner div with two separate ng-repeats. The issue with this is it necessarily repeats the outer div, which causes a 'break' at the end of every outer div repeat, when I just want the inner div repeated as individual units.
My HTML currently looks like this:
<span ng-repeat="item in clubData" layout="row" layout-wrap>
<md-card ng-repeat="player in item.squad" layout="row" layout-wrap>
<md-card-header>
<md-card-avatar>
<div class="clubavatar club-icon-{{player.clubCode}}"></div>
</md-card-avatar>
<md-card-header-text>
<span class="md-title">{{player.firstName + " " + player.lastName}}</span>
<span class="md-subhead"><span class="flag-icon flag-icon-{{player.nationality}} nationid md-whiteframe-1dp"></span>{{player.country}}</span>
</md-card-header-text>
</md-card-header>
</md-card>
</span>
My attempted (and failed) solution:
Ideally, I want to come up with an expression in ng-repeat that will iterate through the elements but only print the squad arrays.
I thought something like this might work (but it doesn't):
<md-card ng-repeat="player in clubData[$index].squad">{{player.name}}</md-card>
Logically (to me) that would print out...
<md-card ng-repeat="player in clubData[0].squad">{{player.name}}</md-card>
<md-card ng-repeat="player in clubData[1].squad">{{player.name}}</md-card>
<md-card ng-repeat="player in clubData[2].squad">{{player.name}}</md-card>
Perhaps I'm misusing $index?
If there's something I'm missing, or an alternative working solution, would really appreciate someone's help.
Thanks in advance.
TypeError: Cannot read property 'forEach' of undefined. 'Undefined' in this instance beingclubData. I checked the clubData array though and it's fine, I called in it in an expression and it printed the whole thing. Do you know why this is not working?