gpt4 book ai didi

javascript - Angular.js错误: Duplicates in a repeater are not allowed - Track by index doesn't work

转载 作者:行者123 更新时间:2023-11-28 19:08:12 25 4
gpt4 key购买 nike

这是我的 Angular JS 和 ionic 框架应用程序的代码。

代码:

HTML 代码:

<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代码:

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
}]

}

问题:

  1. 为什么会出现这个错误?我看不到我的 json 中有任何重复项。我也尝试过通过 $index 进行跟踪,但它不起作用。实际上它消除了重复的错误,但我可以看到很多空的复选框。

  2. 到目前为止,我得到的 "applicationname" 值为 "Facebook"。实际上我只想将其作为 Facebook 。解析json时我应该改变什么。

如有任何帮助,我们将不胜感激。

最佳答案

$scope.app_name = JSON.stringify($scope.applicationdata.applicationname);$scope.devList = JSON.stringify($scope.applicationdata.permissions); 看起来都很可疑。看起来您正在获取 JSON 数据,并将其字符串化。

关于问题#1,看来 ng-repeat 可能正在循环一个字符串,它将其视为一个字符数组。在不知道 $scope.applicationdata.applicationname$scope.applicationdata.permissions 的确切内容的情况下,我不能肯定地说,但这肯定会导致您的问题。

关于问题#2,我不是 100% 确定你的意思。

关于javascript - Angular.js错误: Duplicates in a repeater are not allowed - Track by index doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31274743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com