繁体   English   中英

遍历车把中的对象数组不起作用

[英]iterating over array of objects in Handlebars not working

所以我试图遍历服务器作为json对象返回的数组。 我已经做过一次了,而且效果很好,但是这里的另一个似乎不起作用。

所以把手代码看起来像这样

  <script id="messageTemplate" type="text/x-handlebars-template">
{{#each response.data}}
<div class="message">
  {{#messages}}
  <h4>{{this.from}}</h4>
  <b>{{this.datetime}}</b>
  <p>{{this.body}}</p>
  {{/messages}}
</div>
{{/each}}
</script>

和我试图像这样迭代的json对象

 {
 "data": {
"id": 32607158,
"with_account": "user1",
"with_account_id": 8483786,
"last_message_preview": "XD",
"message_count": 289,
"messages": [{
  "id": 153359194,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711004
}, {
  "id": 153359270,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711023
}, {
  "id": 153359330,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711044
}, {
  "id": 153359334,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711048
}, {
  "id": 153359386,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711057
}, {
  "id": 153359430,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711065
}, {
  "id": 153359474,
  "from": "user1",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711078
}, {
  "id": 153359522,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 20094939,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711090
}, {
  "id": 153359638,
  "from": "user2",
  "account_id": 20094939,
  "sender_id": 8483786,
  "body": "*hugs*",
  "conversation_id": 32607158,
  "datetime": 1439711106
}],
"done": false,
"page": 2,
"datetime": 1439711387
},
"success": true,
"status": 200
}

有人知道为什么它不起作用吗? 在此先感谢您的帮助。

你们each都在错误的地方。 data是单个对象,因此您无需对其进行迭代。 您可以使用with语句获取data

                {{#with response.data}}
                <div class="message">
                    {{#each messages}}
                    <h4>{{this.from}}</h4>
                    <b>{{this.datetime}}</b>
                    <p>{{this.body}}</p>
                    {{/each}}
                </div>
                {{/with}}

如果使用响应对象编译模板,则with将变为

{{#with data}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM