【问题标题】:Error: [ngRepeat:dupes] what does this mean?错误:[ngRepeat:dupes] 这是什么意思?
【发布时间】:2019-04-21 14:22:42
【问题描述】:

repeat 指令从 api 输出葡萄酒记录。我有一个工厂函数来提供葡萄酒 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

这是什么意思? wine 与“wines”不同,为什么它认为它是重复的?

【问题讨论】:

    标签: duplicates angularjs-ng-repeat


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢,但这对我来说仍然没有意义。我还在 HTTP 请求的末尾添加了greatwines.9000.com/wines,由于某种原因它现在可以工作了。我不断遇到这些神秘的错误并使用 angularjs 进行修复。不是很一致。
    【解决方案2】:

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

    它看起来像这样

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

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 2011-03-01
      相关资源
      最近更新 更多