角度ng重复错误"不允许在转发器中重复".

Din*_*ine 466 angularjs ng-repeat

我正在定义一个自定义过滤器:

<div class="idea item" ng-repeat="item in items" isoatom>    
    <div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
        ....
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,使用过滤器的ng-repeat嵌套在另一个ng-repeat中

过滤器的定义如下:

myapp.filter('range', function() {
    return function(input, min, max) {
        min = parseInt(min); //Make string input int
        max = parseInt(max);
        for (var i=min; i<max; i++)
            input.push(i);
        return input;
    };
});
Run Code Online (Sandbox Code Playgroud)

我越来越:

错误:不允许在转发器中重复.Repeater:在item.comments中发表评论| 范围:1:2 ngRepeatAction @ https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an

Web*_*net 949

该解决方案实际上在这里描述:http://www.anujgakhar.com/2013/06/15/duplicates-in-a-repeater-are-not-allowed-in-angularjs/

AngularJS不允许在ng-repeat指令中重复.这意味着如果您尝试执行以下操作,则会出现错误.

// This code throws the error "Duplicates in a repeater are not allowed.
// Repeater: row in [1,1,1] key: number:1"
<div ng-repeat="row in [1,1,1]">
Run Code Online (Sandbox Code Playgroud)

但是,稍微更改上面的代码以定义索引以确定下面的唯一性将使其再次工作.

// This will work
<div ng-repeat="row in [1,1,1] track by $index">
Run Code Online (Sandbox Code Playgroud)

官方文档在这里:https://docs.angularjs.org/error/ngRepeat/dupes

  • 我想提一下,您可以应用任何自定义跟踪属性来定义唯一性,而不仅仅是$ index.因为在这种情况下,对象没有任何其他属性,这可以正常工作.发生此错误的原因是angular使用字典将项的id存储为键,并将值作为DOM引用.从代码(angular.js中的第15402行)看起来,它们基于其关键字作为性能优化来缓存先前找到的DOM元素.由于它们需要唯一键,因此当它们在15417行找到重复键时会明确地抛出此错误 (48认同)
  • 哎呀,Angular的那个毫无意义的讨厌设计.在开发和测试过程中很容易错过这个问题,因为你的数组从不包含重复项,只是让你的应用程序第一次暴露在dupes中时会在生产中爆炸.我之前建立了Angular应用程序而不知道"没有欺骗"限制; 我现在发现自己回想起来,想知道我是否在不知不觉中创作了破碎的代码. (21认同)
  • `[div ng-repeat ="行在[1,1,1,2,2] |过滤器:2跟踪$ index">``搜索过滤器'需要在"跟踪$ index"之前 (16认同)
  • @Shlomi 明白了。请注意“track by $index”有一些[大陷阱](/sf/answers/3200634951/)。 (2认同)

use*_*Bee 45

对于那些期望JSON并仍然得到相同错误的人,请确保您解析数据:

$scope.customers = JSON.parse(data)
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用,我希望json在使用$ http服务后,但是SyntaxError:Object.parse(native)上的意外标记o (4认同)
  • 疯.经过几个月的正常工作,Angular突然决定从$ http()返回的JSON不再是JSON.明确地解析它为我们解决了这个问题.谢谢你的疯狂建议. (2认同)

Mah*_*wal 11

我在我的项目中遇到了一个问题,我在$ index中使用了ng-repeat track,但是当数据来自数据库时,产品没有得到反映.我的代码如下:

<div ng-repeat="product in productList.productList track by $index">
  <product info="product"></product>
 </div>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,product是一个单独的指令来显示产品.但是我发现当我们从作用域传出数据时$ index会导致问题.因此数据丢失和DOM无法更新.

我通过使用product.id作为ng-repeat中的键来找到解决方案,如下所示:

<div ng-repeat="product in productList.productList track by product.id">
  <product info="product"></product>
 </div>
Run Code Online (Sandbox Code Playgroud)

但是当多个产品具有相同的ID时,上面的代码再次失败并抛出以下错误:

angular.js:11706错误:[ngRepeat:dupes]不允许在转发器中重复.使用'track by'表达式指定唯一键.中继器

所以最后我通过制作ng-repeat的动态唯一键来解决问题,如下所示:

<div ng-repeat="product in productList.productList track by (product.id + $index)">
  <product info="product"></product>
 </div>
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题,并希望这将在未来帮助你.


Eze*_*tor 5

您希望"范围"过滤器做什么?

这是我认为你想要做的工作样本:http://jsfiddle.net/evictor/hz4Ep/

HTML:

<div ng-app="manyminds" ng-controller="MainCtrl">
  <div class="idea item" ng-repeat="item in items" isoatom>    
    Item {{$index}}
    <div class="section comment clearfix" ng-repeat="comment in item.comments | range:1:2">
      Comment {{$index}}
      {{comment}}
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

angular.module('manyminds', [], function() {}).filter('range', function() {
    return function(input, min, max) {
        var range = [];
        min = parseInt(min); //Make string input int
        max = parseInt(max);
        for (var i=min; i<=max; i++)
            input[i] && range.push(input[i]);
        return range;
    };
});

function MainCtrl($scope)
{
    $scope.items = [
        {
            comments: [
                'comment 0 in item 0',
                'comment 1 in item 0'
            ]
        },
        {
            comments: [
                'comment 0 in item 1',
                'comment 1 in item 1',
                'comment 2 in item 1',
                'comment 3 in item 1'
            ]
        }
    ];
}
Run Code Online (Sandbox Code Playgroud)