2015-10-08 44 views
6

重複指令從api輸出葡萄酒記錄。我有一個工廠函數就可以提供酒API,然後在我的控制器訪問錯誤:[ngRepeat:dupes]這是什麼意思?

app.factory("Wine", function ($http){ 
    var factory = {}; 

    //getWines 
    factory.getWines = function(){ 
     return $http.get("http://www.greatwines.9000.com") 
    } 

} 

控制器:

app.controller("winesCtrl", function($scope, $http, Wine){ 
     Wine.getWines() 
     .success(function(wines){ 
      $scope.wines = wines; 
     }) 
     .error(function(){ 
      alert("Error!"); 
     }); 
    }); 

VIEW: 

<h2>Wine list</h2> 
    <div class="row margin-top-20 wine-container" ng-repeat="wine in wines"> 
     <div class="col-sm-3"> 
      <img src="{{wine.picture}}" class="img-responsive" /> 
     </div> 
     <div class="col-sm-9"> 
      <div class="margin-top-20"> 
       <span class="bold">Name: </span><span>{{wine.name}}</span> 
      </div> 
      <div> 
       <span class="bold">Year: </span><span>{{wine.year}}</span> 
      </div> 
      <div> 
       <span class="bold">Grapes: </span><span>{{wine.grapes}}</span> 
      </div> 
      <div> 
       <span class="bold">Country: </span><span>{{wine.country}}</span> 
      </div> 
      <div> 
       <span class="bold">Region: </span><span>{{wine.region}}</span> 
      </div> 
      <div> 
       <span class="bold">Price: </span><span>{{wine.price}}</span> 
      </div> 
      <div> 
       <span class="bold">{{wine.description}}</span> 
      </div> 
      <div class="margin-top-20"> 
       <a href="#/wines/{{wine.id}}" class="btn btn-default">Edit Wine</a> 
      </div> 
     </div> 
    </div> 

我對這個錯誤,並在典型點擊「模糊」 angularjs時尚我得到這個:

Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: wine in wines, Duplicate key: string:e, Duplicate value: e 

這是什麼意思?葡萄酒與「葡萄酒」不一樣,爲什麼它認爲它是重複的?

回答

10

在ngRepeat表達式中存在重複鍵時發生。重複鍵被禁止,因爲AngularJS使用鍵來關聯DOM節點和項目。

這意味着$ scope.wines有一些重複的值。

您也可以參考這個帖子:Angular ng-repeat Error "Duplicates in a repeater are not allowed."

+0

謝謝,但它仍然沒有道理給我。此外,我還在HTTP請求的末尾添加了http://www.greatwines.9000.com/wines,出於某種原因,它現在可以工作。我不斷遇到這些神祕的bug並修復了angularjs。這不是很一致。 – HGB