问题
I'm trying to create a simple category with items kind of setup, where the categories will be all my albums with all the tracks. I've tried to set it up like this:
enitoniApp.controller('musicItems', ['$scope', function ($scope) {
$scope.albums = [
{
name: 'Over The Mountains',
tracks: [
{
name: 'Over The Mountains',
ref: 'otmt',
released: 0,
price: 0,
},
{
name: '!C3',
ref: 'ice',
released: 0,
price: 0,
}
]
}
]
}]);
and in the view:
<body>
<div id="allMusic" ng-controller="musicItems">
<div id="albums">
<ul>
<li ng-repeat="album in albums">
<div class="title">{{album.name}}</div>
<ul>
<li ng-repeat="track in albums.tracks">{{ track.name}}</li>
</ul>
</li>
</ul>
</div>
</div>
</body>
The expected behaviour is that angular.js will repeat the li
's inside the album li
based on how many tracks, which in this case is two. However, I cannot get it to access the amount of array items inside tracks
.
This is what I am trying to achieve:
<body>
<div id="allMusic" ng-controller="musicItems">
<div id="albums">
<ul>
<li>
<div class="title">Over The Mountains</div>
<ul>
<li>Over the mountains</li>
<li>!C3</li>
</ul>
</li>
</ul>
</div>
</div>
</body>
回答1:
Inner ngRepeat
should be track in album.tracks
(you have albums):
<li ng-repeat="track in album.tracks">{{ track.name}}</li>
<!-- extra "s" was here -----^ -->
来源:https://stackoverflow.com/questions/31776769/access-items-from-array-within-object-property-using-ng-repeat