2016-05-16 70 views
2

我正在學習平均值,並且遇到了一個Angular錯誤。我想在一個表格中顯示錶格數據,但是數據是不會通過,我收到的錯誤是:Angular ngRepeat:dupes錯誤(雖然沒有重複的鍵)

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(); 
     }); 
    } 

而且在視圖文件中:

<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的曲目。在過濾器之前添加時,它將一堆空行添加到表中。在過濾器後插入時,沒有變化。

+0

不知道這是有關這個問題,但我想你想改變調用usersFactory.index分配的結果,$範圍.users而不是$ scope.data。 –

回答

0

您的兩次服務電話之間存在競爭狀態。無論哪一個完成,都會將結果賦給$ scope.topics。我假設調用用戶的服務應該是這樣的:

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

謝謝!這照顧了ngDupes錯誤。 –

+0

很酷。投票和接受會很好。 :) –

1

錯誤[ngRepeat:dupes]是因爲名稱暗示有重複的索引值時收到的。

我建議你使用簡單的console.log(output);來調試接收到的$ http.post中的數據記住,序列化你的ng-repeat數組是一個很好的練習,可以防止重複錯誤。

這將解決空行太

如果你認爲你的反應是正確形成嘗試粘貼正是你正在recieving

0

track by $index的調試應在ngRepeat所有其他表達式後加入。從Angular docs

Note: track by must always be the last expression

我會懷疑你看空行時,最後加入您的軌道由式(而不是反過來在你的問題中指出)。這很可能意味着您的topics陣列中有空或未定義的對象。

我建議你檢查你的工廠方法收到的數據。

相關問題