Angular.js错误:在一个中继器不允许重复 - 通过跟踪指数不工作 [英] Angular.js Error: Duplicates in a repeater are not allowed - Track by index doesn't work
问题描述
下面是我的code的角度js和离子型框架的应用程序。
Here is my code for angular js and ionic framework application.
code:
HTML code:
HTML code:
<ion-header-bar class="bar-calm">
<h1 class="title">Application Permissions</h1>
</ion-header-bar>
<ion-nav-view name="home">
<div class="bar bar-subheader bar-positive">
<h3 class="title"> {{app_name }}</h3>
</div>
<ion-content class="has-subheader">
<div class="list" ng-controller="CheckboxController">
<ion-checkbox ng-repeat="item in devList track by item.text" ng-model="item.checked" ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)">
{{ item.text }}
<h3 class="item-text-wrap"> {{ item.details }}</h3>
</ion-checkbox>
<div class="item">
<pre ng-bind="selection | json"></pre>
</div>
<div class="item">
<pre ng-bind="selection1 | json"></pre>
</div>
</div>
</ion-content>
<ion-footer-bar align-title="left" class="bar-light" ng-controller="FooterController">
<div class="buttons">
<button class="button button-balanced" ng-click="infunc()"> Install </button>
</div>
<h1 class="title"> </h1>
<div class="buttons" ng-click="doSomething()">
<button class="button button-balanced"> Cancel </button>
</div>
</ion-footer-bar>
</ion-nav-view>
JS code:
JS code:
pmApp.controller('CheckboxController', function ($scope, $http, DataService) {
// define the function that does the ajax call
getmydata = function () {
return $http.get("js/sample.json")
.success(function (data) {
$scope.applicationdata = data;
});
}
// do the ajax call
getmydata().success(function (data) {
// stuff is now in our scope, I can alert it
$scope.app_name = JSON.stringify($scope.applicationdata.applicationname);
$scope.devList = JSON.stringify($scope.applicationdata.permissions);
console.log("Application Name : " + $scope.app_name);
console.log("Permissions : " + $scope.devList);
});
$scope.selection = [];
$scope.selection1 = [];
// toggle selection for a given employee by name
$scope.toggleSelection = function toggleSelection(item) {
var idx = $scope.selection.indexOf(item);
var jsonO = angular.copy(item);
jsonO.timestamp = Date.now();
DataService.addTrackedData(jsonO);
$scope.selection1 = DataService.getTrackedData();
// is currently selected
if (idx > -1) {
$scope.selection.splice(idx, 1);
}
// is newly selected
else {
DataService.addSelectedData(item);
$scope.selection = DataService.getSelectedData();
/* $scope.selection.push(item);*/
}
};
});
错误:
Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: item in devList track by item.text, Duplicate key: undefined, Duplicate value: {
JSON:
{
"applicationname": "Facebook",
"permissions": [
{
"text": "Device & app history",
"details": "Allows the app to view one or more of: information about activity on the device, which apps are running, browsing history and bookmarks",
"checked": "false"
},
{
"text": "Identity",
"details": "Uses one or more of: accounts on the device, profile data",
"checked": false
}]
}
问题:
-
为什么这个错误来了?我看不出有我的JSON任何重复。我也$索引,但它不工作试过轨道。实际上,它消除了重复的错误,但我可以看到很多空的复选框。
Why this error came? I can not see there is any duplicate in my json. I also tried track by $index but its not working. Actually it removes the duplicate error but I can see a lot empty checkboxes.
截至目前,我正在的applicationName
值脸谱
。其实我想把它当作的Facebook
而已。我应该在JSON解析更改。
As of now I am getting "applicationname"
value as "Facebook"
. Actually I want it as Facebook
only. What should I change in parsing of json.
任何帮助将是AP preciated。
Any help would be appreciated.
推荐答案
$ scope.app_name = JSON.stringify($ scope.applicationdata.applicationname);
和 $ scope.devList = JSON.stringify($ scope.applicationdata.permissions);
似乎都腥。它看起来像你得到JSON数据和字符串化-ING它。
$scope.app_name = JSON.stringify($scope.applicationdata.applicationname);
and $scope.devList = JSON.stringify($scope.applicationdata.permissions);
both seem fishy. It looks like you're getting JSON data, and stringify-ing it.
关于问题#1,它出现NG-重复可能是遍历字符串,它把作为一个字符数组。如果不知道的 $ scope.applicationdata.applicationname
和 $ scope.applicationdata.permissions
我不能说的确切内容肯定的,但这肯定会导致您的问题。
With regard to question #1, it appears ng-repeat was probably looping over a string, which it treats as an array of characters. Without knowing the exact contents of $scope.applicationdata.applicationname
and $scope.applicationdata.permissions
I can't say for sure but this would definitely cause your issue.
对于问题2,我不是100%肯定你的意思了。
With regard to question #2, I'm not 100% sure what you mean.
这篇关于Angular.js错误:在一个中继器不允许重复 - 通过跟踪指数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!