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

标签 javascript angularjs json cordova

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

相关文章:

javascript - 使用 Jasmine 测试 Angular Controller 的 promise

python - PySpark - 如何输出具有特定字段的 JSON?

PHP json_解码 : Object to Associative Array

javascript - 向 Instagram feed 添加分页或延迟加载

javascript - 如何从 php 返回的 JSON 中检索值

javascript - Twitter 如何实时更新您的时间线?

javascript - 将 bool 值传递给指令

javascript - 将 SweetAlert2 与 Angular 一起使用时如何保持范围?

javascript - 如何从相同 ng-options 的其他用途中删除 select ng-options

python - 如何修复在 Python 中使用 REST 连接到 SharePoint 并读取数据时的 JSON 解码器问题