
[英]Angular.js Error: Duplicates in a repeater are not allowed - Track by index doesn't work
[英]Duplicates in a repeater are not allowed angular.js
我有一个带有以下代码的视图控制器,该代码可从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]不允许在转发器中重复。 使用“跟踪依据”表达式指定唯一键。 中继器:由post.link在last_news_posts轨道中发布,重复键:函数link(),重复值:
我阅读了文章并通过添加了track by
,但这没有帮助。 我想这可能是因为ng-repeat
函数没有及时完成来自服务器的新数据。 但是不知道该如何解决。
检查来自API的数据。 可能有些项目内部具有完全相同的post.link。 如果您使用跟踪方式,那么用于跟踪的项目就必须是唯一的!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.