【问题标题】:Duplicates in a repeater are not allowed angular.js不允许在转发器中重复 angular.js
【发布时间】:2017-11-08 04:31:50
【问题描述】:

我有一个带有以下代码的视图控制器,它从 api 服务器获取数据:

$scope.recent_news_posts = localStorageService.get('recent_news_posts') || [];


        $http({method: 'GET', url: 'http://myapi.com/posts'}).success(function(data) {
            if ($scope.$$destroyed) {
                return
            }

            $scope.recent_news_posts = data || [];
            localStorageService.set("recent_news_posts", $scope.recent_news_posts);
        });

在模板中:

<md-card class="news-item" ng-repeat="post in recent_news_posts track by post.link" ng-click="openExternalLink(post.link);">
                    <md-card-title>
                        <md-card-title-text>
                            <span class="md-headline">{{ post.title }}</span>
                            <div class="md-subhead" ng-bind-html="post.content"></div>
                        </md-card-title-text>
                        <md-card-title-media>
                            <img src="{{ post.image }}" class="md-media-lg">
                        </md-card-title-media>
                    </md-card-title>
                </md-card>

有时我会收到来自哨兵的报告,例如:

[ngRepeat:dupes] 不允许在转发器中重复。使用'跟踪 by' 表达式来指定唯一键。中继器:张贴在 recent_news_posts 由 post.link 跟踪,重复键:函数链接(), 重复值:

http://errors.angularjs.org/1.5.9/ngRepeat/dupes?p0=post%20in%20recent_news_posts%20track%20by%20post.link&p1=function%20link()&p2=%0A

我阅读了文章并添加了track by,但这并没有帮助。 我想这可能是因为ng-repeat 函数没有及时完成来自服务器的新数据。但不知道如何解决这个问题。

【问题讨论】:

  • 试试track by $index

标签: javascript angularjs


【解决方案1】:

检查来自 API 的数据。可能有些项目内部具有完全相同的 post.link。如果您使用 track ,那么用于跟踪的项目必须是唯一的!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多