AngularJS错误:[ngRepeat:dupes]

时间:2015-12-20 09:03:56

标签: php angularjs json

我正在使用PHP获取JSON数据但是在ng-repeat获取错误:

[ngRepeat:dupes] Duplicates in repeater are not allowed

我尝试track by $index,但每次浏览器都与track by $index崩溃时,在localhost中完美运行,但在虚拟主机上遇到上述错误。

任何解决方案?

JSON数据:

{
  id : "1",
  title : "Title1",
  src : "Source1"
},
{
  id : "2",
  title : "Title2",
  src : "Source2"
}

1 个答案:

答案 0 :(得分:2)

你可以尝试这样的事情。在控制器范围中定义方法:

$scope.getIndexFunction = function($index, data) {
    // Generate a unique string so that ng-repeat can avoid error
    return $index + '' + data.toString();
};

现在,在您看来:

<div ng-repeat="source in sources track by getIndexFunction($index, source)"> {{source.title}}</div>

Directive doesn't fire after changing textarea model