错误:[ngRepeat:dupes]这是什么意思?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误:[ngRepeat:dupes]这是什么意思?相关的知识,希望对你有一定的参考价值。

从api输出wine记录的repeat指令。我有一个工厂功能来提供葡萄酒API,然后在我的控制器中访问

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

这是什么意思?葡萄酒与“葡萄酒”不一样,为什么它认为它是重复的?

答案

在ngRepeat表达式中存在重复键时发生。禁止重复键,因为AngularJS使用键将DOM节点与项目关联。

这意味着$ scope.wines有一些重复的值。

你也可以参考这篇文章:Angular ng-repeat Error "Duplicates in a repeater are not allowed."

另一答案

确实,AngularJS使用键将DOM节点与项目关联起来。所以,你可以通过添加"track by $index"来解决。

它看起来像这样

ng-repeat="wine in wines track by $index"

以上是关于错误:[ngRepeat:dupes]这是什么意思?的主要内容,如果未能解决你的问题,请参考以下文章

Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed.报错

angularjs基础ng-repeat嵌套循环报错angular.min.js:89 Error: [ngRepeat:dupes]

[ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to spec

angularJS问题集结

ng-repeat 之 track by $index

AngularJS中track by的作用