Blessan Kurien Blessan Kurien - 6 months ago 52
AngularJS Question

Angular js : ng-repeat display as json string

I am processing a json response in

map
function later it is displayed in an
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
};
});


but when i try to display it in html it will display as full json string.

<div class="list-expense-menu-item" ng-repeat="todo in todos">
{{todo.note}}
</div>


I've tried

$scope.todos=JSON.stringify($scope.todos);


but it will thrown an error message

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


Note:

Without the map function
ng-repeat
works fine

UPDATE
Here is my data

[{"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}]

SSH SSH
Answer

I think you have two nested ng-repaet for this. also i use forEach loop for modify 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>