有没有办法重新映射使用 ng-repeat 迭代的对象的关键值

Is there a way to remap key values for an object iterated using ng-repeat?

本文关键字:迭代 ng-repeat 对象 键值 新映射 映射 有没有      更新时间:2023-09-26

我正在使用angularjs-soundmanager2,对象数组必须符合此对象

{id: n, title: 'Title', artist: 'Artist', url: 'URL'}

正在访问一个为我提供此对象的远程 API

{id: n, filename: 'URL', title: 'Title'}

我想做的是重新映射后一个对象以符合前一个对象。我尝试在 ng-repeat 中使用过滤器,但这似乎不会在没有迭代中止的情况下产生结果(角度给了我这个错误[$rootScope:infdig] 10 $digest() iterations reached. Aborting!)。

app.filter('mapAudio', function() {
    return function(items, field) {
        var filtered = [];
        angular.forEach(items, function(item) {
            filtered.push({id: item.id, title: item.title, artist: '', url: item.filename});
        });
        return filtered;
    };
});

在 HTML 端:

<a ng-repeat="sample in selected.audio | mapAudio" href="#" class="list-group-item">{{sample.title}}</a>

为什么不更新源列表?

<a ng-repeat="sample in getFullModels(selected.audio) | mapAudio" href="#" class="list-group-item">{{sample.title}}</a>

#getFullModels必须编码才能返回完整的模型表示形式。