我正在map
函数中处理json响应,稍后它会显示在ng-repeat
//from api call
$scope.todos=response.data;
//processing
$scope.todos = $scope.todos.map(function(text, status,note,create_date,to_do_id) {
var ch;
if(status=='1') ch=true; else ch=false;
console.log(ch);
return{
text,
flag:ch,
note:note,
to_do_id:to_do_id,
create_date:create_date,
status:status
};
});
但是当我尝试在html 中显示它时,它将显示为完整的json字符串。
<div class="list-expense-menu-item" ng-repeat="todo in todos">
{{todo.note}}
</div>
我试过
$scope.todos=JSON.stringify($scope.todos);
但会抛出错误消息
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: todo in todos, Duplicate key: string:t, Duplicate value: t
注意:
没有地图功能ng-repeat
可以正常工作
更新 这是我的数据
[{"text":{"to_do_id":"53","note":"test todo update","status":"0","is_enabled":"1","create_date":"2016-06-04T21:46:35+0530","completed_date":"2016-06-04T22:17:53+0530","exp_date":"0000-00-00T00:00:00+0530"},"flag":false,"note":[{"to_do_id":"53","note":"test todo update","status":"0","is_enabled":"1","create_date":"2016-06-04T21:46:35+0530","completed_date":"2016-06-04T22:17:53+0530","exp_date":"0000-00-00T00:00:00+0530"},{"to_do_id":"52","note":"new hello todo","status":"0","is_enabled":"1","create_date":"2016-06-04T21:40:30+0530","completed_date":"2016-06-05T00:06:28+0530","exp_date":"0000-00-00T00:00:00+0530"}],"status":0},{"text":{"to_do_id":"52","note":"new hello todo","status":"0","is_enabled":"1","create_date":"2016-06-04T21:40:30+0530","completed_date":"2016-06-05T00:06:28+0530","exp_date":"0000-00-00T00:00:00+0530"},"flag":true,"note":[{"to_do_id":"53","note":"test todo update","status":"0","is_enabled":"1","create_date":"2016-06-04T21:46:35+0530","completed_date":"2016-06-04T22:17:53+0530","exp_date":"0000-00-00T00:00:00+0530"},{"to_do_id":"52","note":"new hello todo","status":"0","is_enabled":"1","create_date":"2016-06-04T21:40:30+0530","completed_date":"2016-06-05T00:06:28+0530","exp_date":"0000-00-00T00:00:00+0530"}],"status":1}]
答案 0 :(得分:2)
我认为你有两个嵌套ng-repaet
。我也使用forEach
循环来修改flag
。
var app = angular.module("app" , []);
app.controller("MyCtrl" , function($scope){
$scope.todos = [
{
"text": {
"to_do_id": "53",
"note": "test todo update",
"status": "0",
"is_enabled": "1",
"create_date": "2016-06-04T21:46:35+0530",
"completed_date": "2016-06-04T22:17:53+0530",
"exp_date": "0000-00-00T00:00:00+0530"
},
"note": [
{
"to_do_id": "53",
"note": "test todo update",
"status": "0",
"is_enabled": "1",
"create_date": "2016-06-04T21:46:35+0530",
"completed_date": "2016-06-04T22:17:53+0530",
"exp_date": "0000-00-00T00:00:00+0530"
},
{
"to_do_id": "52",
"note": "new hello todo",
"status": "0",
"is_enabled": "1",
"create_date": "2016-06-04T21:40:30+0530",
"completed_date": "2016-06-05T00:06:28+0530",
"exp_date": "0000-00-00T00:00:00+0530"
}
],
"status": 0
},
{
"text": {
"to_do_id": "52",
"note": "new hello todo",
"status": "0",
"is_enabled": "1",
"create_date": "2016-06-04T21:40:30+0530",
"completed_date": "2016-06-05T00:06:28+0530",
"exp_date": "0000-00-00T00:00:00+0530"
},
"note": [
{
"to_do_id": "53",
"note": "test todo update",
"status": "0",
"is_enabled": "1",
"create_date": "2016-06-04T21:46:35+0530",
"completed_date": "2016-06-04T22:17:53+0530",
"exp_date": "0000-00-00T00:00:00+0530"
},
{
"to_do_id": "52",
"note": "new hello todo",
"status": "0",
"is_enabled": "1",
"create_date": "2016-06-04T21:40:30+0530",
"completed_date": "2016-06-05T00:06:28+0530",
"exp_date": "0000-00-00T00:00:00+0530"
}
],
"status": 1
}
];
angular.forEach($scope.todos,function(todo){
if(todo.status == 1)
todo.flag = true;
else
todo.flag = false;
})
console.log($scope.todos);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
<div class="list-expense-menu-item" ng-repeat="todo in todos">
<div ng-repeat="note in todo.note">
<span>{{note.to_do_id}}</span> |
<span>{{note.note}}</span>|
<span>{{note.status}}</span>|
<span>{{note.create_date}}</span>
</div>
</div>
</div>