Angularjs 角度ng重复误差“;中继器中不允许有重复项。”;
我定义了一个自定义过滤器,如下所示:Angularjs 角度ng重复误差“;中继器中不允许有重复项。”;,angularjs,ng-repeat,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> .... 正如您所看到的,使用过滤器的
<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;
};
});
{"items":
[
{
"index": 1,
"name": "Samantha",
"rarity": "Scarborough",
"email": "maureen@sykes.mk"
},
{
"index": 2,
"name": "Amanda",
"rarity": "Vick",
"email": "jessica@livingston.mv"
}]
}
myapp.filter('range',function(){
返回功能(输入、最小值、最大值){
min=parseInt(min);//使字符串输入为int
max=parseInt(max);
对于(var i=min;i您希望“范围”过滤器做什么
以下是我认为您正在尝试的工作示例:
HTML:
项目{{$index}
注释{{$index}
{{comment}}
JS:
angular.module('manyminds',[],function(){}).filter('range',function()){
返回功能(输入、最小值、最大值){
var范围=[];
min=parseInt(min);//使字符串输入为int
max=parseInt(max);
对于(var i=min;i,此处实际描述了解决方案:
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]">
//此代码引发错误“不允许在中继器中重复。
//中继器:[1,1,1]中的行键:编号:1“
但是,稍微更改上面的代码以定义一个索引来确定唯一性,如下所示,将使其再次工作
// This will work
<div ng-repeat="row in [1,1,1] track by $index">
//这样就行了
这里有官方文档:对于那些期望JSON但仍然收到相同错误的人,请确保解析数据:
$scope.customers = JSON.parse(data)
如果在使用SharePoint 2010时偶然发生此错误:请重命名.json文件扩展名,并确保更新restService路径。不需要额外的“按$index跟踪”
幸运的是,我被转发到这个理由:
.json在SP2010中成为一种重要的文件类型。SP2010包含某些webservice终结点。这些文件的位置是14hive\isapi文件夹。这些文件的扩展名是.json。这就是它产生此类错误的原因
“只关心json文件的内容是json,而不是其文件扩展名”
一旦文件扩展名被更改,应该全部设置。我的JSON
响应如下:
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;
};
});
{"items":
[
{
"index": 1,
"name": "Samantha",
"rarity": "Scarborough",
"email": "maureen@sykes.mk"
},
{
"index": 2,
"name": "Amanda",
"rarity": "Vick",
"email": "jessica@livingston.mv"
}]
}
因此,我使用了ng repeat=“item in variables.items”
来显示它。我在我的项目中遇到了一个问题,我使用了ng repeat track by$index,但当数据来自数据库时,产品没有得到反映。我的代码如下:
<div ng-repeat="product in productList.productList track by $index">
<product info="product"></product>
</div>
在上面的代码中,product是显示产品的单独指令。但是我知道$index在我们从作用域传递数据时会导致问题。因此数据丢失和DOM无法更新
我在ng中使用product.id作为密钥找到了解决方案,如下所示:
<div ng-repeat="product in productList.productList track by product.id">
<product info="product"></product>
</div>
<div ng-repeat="product in productList.productList track by (product.id + $index)">
<product info="product"></product>
</div>
但当多个产品具有相同id时,上述代码再次失败并抛出以下错误:
angular.js:11706错误:[ngRepeat:dupes]中继器中不允许重复。请使用“track by”表达式指定唯一键。中继器
所以最后我通过制作ng repeat的动态唯一键解决了这个问题,如下所示:
<div ng-repeat="product in productList.productList track by product.id">
<product info="product"></product>
</div>
<div ng-repeat="product in productList.productList track by (product.id + $index)">
<product info="product"></product>
</div>
这解决了我的问题,希望这将在将来对您有所帮助。中继器中不允许重复。请使用“跟踪方式”表达式指定唯一键
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>
姓
{{person.firstName}
{{person.lastName}
{{person1.firstName}
{{person1.lastName}
{{sayHello()}}
var myApp=angular.module(“myApp”,[]);
控制器(“personController”[“$scope”,函数($scope)
{
$scope.people=[{firstName:“F1”,lastName:“L1”},
{姓氏:“F2”,姓氏:“L2”},
{姓氏:“F3”,姓氏:“L3”},
{姓:“F4”,姓:“L4”},
{名字:“F5”,姓氏:“L5”}]
$scope.items=[];
$scope.selectedPerson=$scope.people[0];
$scope.showDetails=函数(ind)
{
$scope.selectedPerson=$scope.people[ind];
$scope.items.push($scope.selectedPerson);
}
$scope.sayHello=函数()
{
返回$scope.items.firstName;
}
}])
中继器中不允许重复。请使用“跟踪依据”表达式指定唯一键。中继器:mydt中的详细信息,重复键:字符串:,重复值:
我面临这个错误,因为我在php api部分中编写了错误的数据库名称
因此,当您从数据库中提取数据时,也可能会发生此错误,您的数据库名称写得不正确。为了防止其他人发生这种情况,我在这里记录了这一点,我之所以会出现此错误,是因为我错误地将ng模型设置为与ng repeat数组相同:
<select ng-model="list_views">
<option ng-selected="{{view == config.list_view}}"
ng-repeat="view in list_views"
value="{{view}}">
{{view}}
</option>
</select>
{{view}}
而不是:
<select ng-model="config.list_view">
<option ng-selected="{{view == config.list_view}}"
ng-repeat="view in list_views"
value="{{view}}">
{{view}}
</option>
</select>
{{view}}
我检查了数组,没有任何重复项,只需仔细检查变量。如果在标记中调用ng repeat,则可以允许重复项。有关参考信息,请参阅此链接。
我想说的是,您可以应用任何自定义跟踪属性来定义唯一性,而不仅仅是$index。因为在这种情况下,对象没有任何其他属性,所以这很好。发生此错误的原因是angular使用字典将项的id存储为键,值存储为DOM引用ode(angular.js中的第15402行)看起来他们正在缓存previou