我在尝试读取以下json和html 中的映射时出现以下错误
js:
searhController.orderlogs.results = JSON.stringify(response.data);
角度:
<tr ng-hide="searhController.searching" ng-repeat="log in searhController.orderlogs.results">
<td>{{log.idTransaction}}</td>
<!-- <td>{{log.amount}}</td>
<td>{{log.clientName}}</td>
<td>{{log.created}}</td>
<td>{{log.currency}}</td>
<td>{{log.discountedAmount}}</td>
<td>{{log.lastUpdate}}</td>
<td>{{log.orderId}}</td> -->
</tr>
JSON:
[{"idTransaction":2081101,"amount":34990.0,"clientName":"Payment hub","created":"ene 12, 2015","currency":"CLP","discountedAmount":34990.0,"lastUpdate":"ene 12, 2015","orderId":"1421094905114","productDescription":"total: 1 item(s)","fop":{"nameFop":"CAT_FAKE"},"application":{"idApplication":10001,"nameApplication":"TEST APPLICATION"},"transactionStatus":{"nameTransactionStatus":"Waiting for reply"},"transactionByFop":{"settled_amount":0.0,"installments_amount":0.0,"installments_number":0}}]
错误:
angular.js:13920 Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: log in searhController.orderlogs.results, Duplicate key: string:a, Duplicate value: a
您的JSON无效,您应该正确格式化它,因为它不是那么小——一行代码非常糟糕。
这将是有效的:
{
"transaction":{
"idTransaction":2081101,
"amount":34990.0,
"clientName":"Payment hub",
"created":"ene 12, 2015",
"currency":"CLP",
"discountedAmount":34990.0,
"lastUpdate":"ene 12, 2015",
"orderId":"1421094905114",
"productDescription":"total: 1 item(s)",
"fop":{
"nameFop":"CAT_FAKE"
},
"application":"",
"idApplication":10001,
"nameApplication":"TEST APPLICATION"
},
"transactionStatus":{
"nameTransactionStatus":"Waiting for reply"
},
"transactionByFop":"",
"settled_amount":0.0,
"installments_amount":0.0,
"installments_number":0
}
我做了什么?
您有两个空属性,它们没有填充任何数据,并破坏您的JSON类:
[INVALID] "attr1" : "attr2" : "valueOfAttr2",
[VALID] "attr1" : "", "attr2":"valueOfAttr2"
并且您有多个Root元素,因此它不是有效的JSON。使用正确的数据进行尝试,并测试它是否有效。
<tr ng-hide="searhController.searching" ng-repeat="log in searhController.orderlogs.results track by $index">
<td>{{log.idTransaction}}</td>
<!-- <td>{{log.amount}}</td>
<td>{{log.clientName}}</td>
<td>{{log.created}}</td>
<td>{{log.currency}}</td>
<td>{{log.discountedAmount}}</td>
<td>{{log.lastUpdate}}</td>
<td>{{log.orderId}}</td> -->
</tr>
只需将track by $index
添加到ng-repeat
的末尾即可。现在,集合中的两个对象相等,默认情况下ng-repeat
按值跟踪。添加track by $index
将根据对象的位置进行跟踪。
这样做,
ng-repeat="log in searhController.orderlogs.results track by $index">