簡體   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