gpt4 book ai didi

javascript - Angular js : ng-repeat display as json string

转载 作者:行者123 更新时间:2023-12-02 14:33:37 24 4
gpt4 key购买 nike

我正在 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

注意:

没有 map 功能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}]

最佳答案

我认为您为此有两个嵌套的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>

关于javascript - Angular js : ng-repeat display as json string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37638361/

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