Access items from array within object property using ng-repeat

半腔热情 提交于 2019-12-24 15:05:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!