【问题标题】:Access items from array within object property using ng-repeat使用 ng-repeat 从对象属性中的数组访问项目
【发布时间】:2015-08-03 05:32:52
【问题描述】:

我正在尝试使用项目类型的设置创建一个简单的类别,其中类别将是我的所有专辑以及所有曲目。我试过这样设置:

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,
                }
        ]
    }
    ]
}]);

在视图中:

<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>

预期的行为是 angular.js 将根据曲目的数量(在本例中为两首)重复专辑 li 中的 li。但是,我无法让它访问tracks 中的数组项的数量。

这就是我想要达到的目标:

<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>

【问题讨论】:

    标签: javascript html angularjs angularjs-ng-repeat


    【解决方案1】:

    内部ngRepeat 应该是track in album.tracks(你有专辑s):

    <li ng-repeat="track in album.tracks">{{ track.name}}</li>
    <!-- extra "s" was here -----^ -->
    

    【讨论】:

    • 哦,哇,现在我觉得自己很愚蠢。感谢您的帮助,没有意识到我可以参考之前的 ng-repeat。这是一个巧妙的技巧。
    猜你喜欢
    • 1970-01-01
    • 2015-03-16
    • 2017-08-18
    • 2015-03-20
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    相关资源
    最近更新 更多