Angular.js错误:不允许在转发器中重复 - 按索引跟踪不起作用

时间:2015-07-07 16:49:50

标签: javascript json angularjs ionic-framework

这是我的角度js和离子框架应用程序的代码。

代码:

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

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时我应该改变什么。

  3. 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

$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%肯定你的意思。

答案 1 :(得分:1)

问题是您使用转发器作为布尔值,因此如果有2个已检查项或2个未检查项,则将它们标记为重复项:

       <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)">

我建议您在包含div中使用ng-repeat,如:

<div ng-repeat="item in devList">
   <ion-checkbox ng-model="item.checked" ng-checked="selection.indexOf(item) > -1" ng-click="toggleSelection(item)">
</div>