AngularJS数组ng-repeat问题

时间:2016-10-01 12:06:49

标签: angularjs json ng-repeat

我有一个来自php的json数据返回:

[
{
    "id": 143,
    "sender": "btknctTR",
    "title": "Batıkan Bu nedir bu bu ne bu",
    "date": "Jun 11, 2016 6:47:21 AM",
    "lastupdatedate": "Jun 11, 2016 1:48:54 PM",
    "category": "Sorular",
    "priority": "Kritik",
    "appointedRole": "Moderatör",
    "archived": true,
    "messages": [
        {
            "sender": "btknctTR",
            "message": "Test message",
            "date": "Jun 11, 2016 1:48:30 PM"
        },
        {
            "sender": "btknctTR",
            "message": "This i second test message",
            "date": "Jun 11, 2016 1:48:54 PM"
        }
    ]
},
{
    "id": 198,
    "sender": "btknctTR",
    "title": "Yeni yeni yeniden",
    "date": "Jun 11, 2016 12:53:32 PM",
    "lastupdatedate": "Jun 11, 2016 8:18:39 PM",
    "category": "Diger",
    "priority": "Normal",
    "appointedRole": "Moderatör",
    "archived": true,
    "messages": [
        {
            "sender": "btknctTR",
            "message": "Yeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yenidenYeni yeni yeniden",
            "date": "Jun 11, 2016 7:54:41 PM"
        },
        {
            "sender": "DonduranAtes",
            "message": "asdasd\r\n",
            "date": "Jun 11, 2016 7:54:50 PM"
        },
        {
            "sender": "btknctTR",
            "message": "sadasdasdasd",
            "date": "Jun 11, 2016 7:55:14 PM"
        }
    ]
},
{
    "id": 3810,
    "sender": "btkncttr",
    "title": "asdasdasdasdas",
    "date": "Aug 6, 2016 10:55:36 AM",
    "lastupdatedate": "Aug 7, 2016 10:33:19 PM",
    "category": "Diger",
    "priority": "Normal",
    "appointedRole": "Moderatör",
    "archived": true,
    "messages": [
        {
            "sender": "btkncttr",
            "message": "This is the message",
            "date": "Aug 6, 2016 10:57:07 AM"
        },
        {
            "sender": "player",
            "message": "This is the message tooo",
            "date": "Aug 6, 2016 10:57:07 AM"
        }
    ]
}
]

但我无法找到如何重复这些。当我写 {{data.sender}} 时,在控制台中返回错误[ngRepeat:dupes]。

我需要在故障单列表页面中列出此值,这是用户的支持系统。

3 个答案:

答案 0 :(得分:0)

假设您的对象位于变量$ scope.data的值中。

然后你可以在html中使用angular渲染它,如下所示:

<div ng-repeat="item in data track by $index">
    {{ item.id }}
</div>

更新:确实,有了这个特定错误,您需要按$ index跟踪

答案 1 :(得分:0)

您在控制台中收到[ngRepeat:dupes]错误,因为数据中存在一些重复项。

如果您要遍历所有数据并显示sender,您可以执行以下操作

<div ng-repeat="data in jsonData track by $index">{{data.sender}}</div>

$index使用数组中对象的索引跟踪数组中的每个对象。

答案 2 :(得分:0)

试试这个, Here is working fiddle
同时检查Angular版本,它应该至少为1.2,因为它是在该版本中引入的。

 <div ng-controller="MyCtrl">
  <div ng-repeat="data in datas track by $index">
    Sender : {{data.sender}}
    <br> Messages :<br>
    <div ng-repeat="(mindex, mvalue) in data.messages">
       {{mindex+1}}. {{mvalue.message}} - {{mvalue.date}}
    </div>
    <br>
  </div>
</div>