绑定json时出错

时间:2014-12-04 05:10:39

标签: json angularjs

我在我的一个程序中使用Angularjs。我用它来添加项目到json数组。然后我将此数组保存到临时文件中。此时程序工作正常。当我尝试将保存的json字符串绑定到表时,我得到错误错误:[ngRepeat:dupes] 在搜索文档时,我发现AngularJS允许通过语法“Track by $ index”重复值,并显示示例重复单维json。我的问题是,如何控制id为

的转发器

HTML代码

<table>
<thead>
    <tr>
        <th style="width: 5%; text-align: left;">
            Edit
        </th>
        <th style="width: 5%; text-align: left;">
            Dr./Cr.
        </th>
        <th style="width: 25%; text-align: left;">
            G.L. Head
        </th>
        <th style="width: 25%; text-align: left;">
            S.L. Head
        </th>
        <th style="width: 25%; text-align: left;">
            Narration
        </th>
        <th style="width: 18%; text-align: right;">
            Amount
        </th>
    </tr>
</thead>
<tbody>
    <tr ng-repeat="TD in TDS track by TD.id">
        <td style="width: 5%; text-align: left;">
            <button class="btn" ng-click="editTD(TD.id)">
                <span class="glyphicon glyphicon-pencil"></span>Edit
            </button>
        </td>
        <td style="width: 5%; text-align: left;">
            {{ TD.dbcr }}
        </td>
        <td style="width: 25%; text-align: left;">
            {{ TD.glhead }}
        </td>
        <td style="width: 25%; text-align: left;">
            {{ TD.slhead }}
        </td>
        <td style="width: 25%; text-align: left;">
            {{ TD.narr }}
        </td>
        <td style="width: 18%; text-align: right;">
            {{ TD.amt }}
        </td>
    </tr>
</tbody>
</table>

jQuery 请注意,如果我删除$ scope。$ apply(),则不会显示任何错误,但也不会显示数据。

$scope.OpenTemp = function () {
    var vFileName = $("#TempList").val();
    $scope.sysmsg = 'Opening voucher from temporary location';
    var to = "../../WHS/OpenTempJV.ashx";
    var options = {
        type: "POST",
        url: to,
        data: { vFile: vFileName },
        error: function () {
            ShowErrorMsg("Error in opening temporary voucher");
        },
        success: function (data) {
            if (data != "") {
                //alert(data.d);
                //$scope.sysmsg = 'success';
                $("#divGrid").show();
                $scope.TDS = $scope.TDS.concat(data);
                //$scope.TDS = data;
                //$scope.$apply();
                //CalcAllTotals(data);
            }
            else {
                ShowErrorMsg("Could not retrieve temporary voucher");
            }
        }
    };
    $.ajax(options);
}

数据中的JSON字符串

[
{
    "id": 1,
    "dbcr": "C",
    "glhead": "A B KING PRO EXCERCISER MACHINE AT COST",
    "glid": "3",
    "slhead": "",
    "slid": "0",
    "narr": "dfasdf",
    "amt": "454",
    "$$hashKey": "004"
},
{
    "id": 2,
    "dbcr": "C",
    "glhead": "A.C. AT COST",
    "glid": "593",
    "slhead": "",
    "slid": "0",
    "narr": "asdfasdf",
    "amt": "32434",
    "$$hashKey": "006"
},
{
    "id": 3,
    "dbcr": "D",
    "glhead": "ADD ON COURSES (BOOKS)",
    "glid": "6",
    "slhead": "",
    "slid": "0",
    "narr": "asdfasdf",
    "amt": "324",
    "$$hashKey": "008"
},
{
    "id": 4,
    "dbcr": "D",
    "glhead": "GROUP INSURANCE SCHEME (STUDENTS)",
    "glid": "270",
    "slhead": "",
    "slid": "0",
    "narr": "adsfasdfadsf",
    "amt": "44324",
    "$$hashKey": "00A"
},
{
    "id": 5,
    "dbcr": "D",
    "glhead": "HRA (TEACHING STAFF)",
    "glid": "357",
    "slhead": "",
    "slid": "0",
    "narr": "asdfadsfasdfds",
    "amt": "11760",
    "$$hashKey": "00C"
},
{
    "id": 6,
    "dbcr": "C",
    "glhead": "EXTENSION CONST. OF ADMN. BLOCK BUILDING",
    "glid": "267",
    "slhead": "",
    "slid": "0",
    "narr": "adfasdfasdf",
    "amt": "23520",
    "$$hashKey": "00E"
}

Chrome错误堆栈

 Error: [ngRepeat:dupes] http://errors.angularjs.org/undefined/ngRepeat/dupes?p0=TD%20in%20TDS%20track%20by%20TD.id&p1=undefined
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:6:453
at https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:182:179
at Object.fn (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:97:409)
at g.$get.g.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:98:304)
at g.$get.g.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js:101:12)
at Object.TDController.options.success (http://localhost:49399/DAV_JV/accounts/transaction/jvAng.js:254:28)
at x.Callbacks.l (http://localhost:49399/DAV_JV/js/jquery.min.js:4:24584)
at Object.x.Callbacks.c.fireWith [as resolveWith] (http://localhost:49399/DAV_JV/js/jquery.min.js:4:25405)
at k (http://localhost:49399/DAV_JV/js/jquery.min.js:6:4709)

0 个答案:

没有答案