gpt4 book ai didi

javascript - Angular ngRepeat : dupes error (although there are no duplicate keys)

转载 作者:行者123 更新时间:2023-11-29 14:42:03 25 4
gpt4 key购买 nike

我正在学习 MEAN,遇到了 Angular 错误。我试图在表格中显示表单数据,但是数据没有通过,我收到的错误是:

angular.js:11500 Error: [ngRepeat:dupes] http://errors.angularjs.org/1.3.5/ngRepeat/dupes?p0=topic%20in%20topics%20%20%7C%20filter%3A%20filter_q&p1=string%3Ap&p2=p at Error (native) at

在我的 index.html 文件中:

discussion_board.controller('dashboardController', function($scope, usersFactory, topicsFactory){
$scope.topics = [];
$scope.users = [];

topicsFactory.index(function(data){
$scope.topics = data;
})

usersFactory.index(function(data){
$scope.topics = data;
})

$scope.addtopic = function(){
console.log("from controller)");
topicsFactory.addTopic($scope.new_topic, function(topics){
$scope.topics = topics;
$scope.new_topic = {};
});
}
})
discussion_board.factory('topicsFactory', function($http){
var factory = {};
var users = [];

factory.index = function(callback) {
$http.get('/topics').success(function(output){
callback();
});
}

factory.addTopic = function(info, callback) {
console.log("from factory");
$http.post('/topics', info).success(function(output){
callback();
});
}

在 View 文件中:

<div ng-controller="dashboardController">
<h2>Welcome {{username}} !</h2>
<h5>Search:
<input type="text" ng-model="filter_q" placeholder="search">
</h5>
<table class="table">
<tr>
<th>Category</th>
<th>Topic</th>
<th>User Name</th>
<th>Posts</th>
</tr>
<tr ng-repeat="topic in topics | filter: filter_q">
<td>{{topic.category}}</td>
<td>{{topic.name}}</td>
<td>{{topic.username}}</td>
<td>{{topic.posts}}</td>
</tr>

</table>

我在 ng-repeat 中添加了 $index 跟踪。在过滤器之前添加时,它会向表中添加一堆空行。在过滤器之后插入时,没有变化。

最佳答案

您的两个服务调用之间存在竞争条件。最后完成的那个将把它的结果分配给 $scope.topics。我假设对用户服务的调用应该如下所示:

 usersFactory.index(function(data){
$scope.users= data;
})

关于javascript - Angular ngRepeat : dupes error (although there are no duplicate keys),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37262089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com