我定义了一个自定义过滤器,如下所示:
<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>
正如您所看到的,使用过滤器的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;
};
});
我得到:
错误:在中继器中不允许重复。Repeater: comment in item.comments | range:1:2 ngRepeatAction@https://ajax.googleapis.com/ajax/libs/angularjs/1.1.4/an
我在我的项目中遇到了一个问题,我使用ng-repeat track by $index,但当数据来自数据库时,产品没有得到反映。我的代码如下:
<div ng-repeat="product in productList.productList track by $index">
<product info="product"></product>
</div>
在上面的代码中,product是一个单独的显示产品的指令。但我开始知道,$index导致问题时,我们传递数据从范围。因此数据丢失和DOM无法更新。
我通过使用产品找到了解决方案。Id作为ng-repeat中的一个键,如下所示:
<div ng-repeat="product in productList.productList track by product.id">
<product info="product"></product>
</div>
但是当多个产品具有相同的id时,上面的代码再次失败并抛出下面的错误:
angular.js:11706 Error: [ngRepeat:dupes]在中继器中不允许重复。使用'track by'表达式指定唯一的键。中继器
所以最后我通过ng-repeat的动态唯一键来解决这个问题,如下所示:
<div ng-repeat="product in productList.productList track by (product.id + $index)">
<product info="product"></product>
</div>
这解决了我的问题,希望这对你将来有所帮助。
我的JSON回复是这样的:
{
"items": [
{
"index": 1, "name": "Samantha", "rarity": "Scarborough","email": "maureen@sykes.mk"
},{
"index": 2, "name": "Amanda", "rarity": "Vick", "email": "jessica@livingston.mv"
}
]
}
因此,我在变量中使用ng-repeat = "item。项目”来展示它。
中继器中不允许重复。使用'track by'表达式指定唯一的键。
Repeater: {0}, Duplicate key: {1}, Duplicate value: {2}
例子
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<script src="angular.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="personController">
<table>
<tr> <th>First Name</th> <th>Last Name</th> </tr>
<tr ng-repeat="person in people track by $index">
<td>{{person.firstName}}</td>
<td>{{person.lastName}}</td>
<td><input type="button" value="Select" ng-click="showDetails($index)" /></td>
</tr>
</table> <hr />
<table>
<tr ng-repeat="person1 in items track by $index">
<td>{{person1.firstName}}</td>
<td>{{person1.lastName}}</td>
</tr>
</table>
<span> {{sayHello()}}</span>
</div>
<script> var myApp = angular.module("myApp", []);
myApp.controller("personController", ['$scope', function ($scope)
{
$scope.people = [{ firstName: "F1", lastName: "L1" },
{ firstName: "F2", lastName: "L2" },
{ firstName: "F3", lastName: "L3" },
{ firstName: "F4", lastName: "L4" },
{ firstName: "F5", lastName: "L5" }]
$scope.items = [];
$scope.selectedPerson = $scope.people[0];
$scope.showDetails = function (ind)
{
$scope.selectedPerson = $scope.people[ind];
$scope.items.push($scope.selectedPerson);
}
$scope.sayHello = function ()
{
return $scope.items.firstName;
}
}]) </script>
</body>
</html>