Javascript 角度重复:重复错误(尽管没有重复的关键点)
我正在学习平均值,并且遇到了角度误差。我试图在表中显示表单数据,但数据未通过,收到的错误是: angular.js:11500错误:[ngRepeat:dups] 错误(本机) 在 在index.html文件中,我有:Javascript 角度重复:重复错误(尽管没有重复的关键点),javascript,angularjs,Javascript,Angularjs,我正在学习平均值,并且遇到了角度误差。我试图在表中显示表单数据,但数据未通过,收到的错误是: angular.js:11500错误:[ngRepeat:dups] 错误(本机) 在 在index.html文件中,我有: discussion_board.controller('dashboardController', function($scope, usersFactory, topicsFactory){ $scope.topics = []; $scope.use
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();
});
}
并在视图文件中:
<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>
欢迎{{username}}!
搜索:
类别
话题
用户名
帖子
{{topic.category}}
{{topic.name}
{{topic.username}
{{topic.posts}}
我在ng repeat中添加了track by$index。当添加到筛选器之前时,它会向表中添加一堆空行。当插入过滤器后,没有变化。当名称表明存在重复的索引值时,将接收错误
[ngRepeat:dups]
我建议您使用一个简单的console.log(输出)调试在$http.post中接收的数据代码>请记住,序列化ng repeat数组是防止重复错误的良好做法
这也将解决空行问题
如果您认为您的响应格式正确,请尝试粘贴一个调试,以确定您正在接收的内容。应在ngRepeat
中的所有其他表达式之后添加$index
。发件人:
注意:track by必须始终是最后一个表达式
我怀疑在最后添加track by表达式时,您会看到空行(而不是问题中指出的相反方向)。这很可能意味着主题
数组中有空或未定义的对象
我建议您检查从工厂方法收到的数据。您的两次服务调用之间存在竞争条件。最后完成的将把结果分配给$scope.topics。我假设对用户服务的调用应该如下所示:
usersFactory.index(function(data){
$scope.users= data;
})
不确定这是否与问题有关,但我认为您希望更改对usersFactory.index的调用,以将结果分配给$scope.users,而不是$scope.data。