2013-04-17 48 views
3

我無法使用_.template爲每個註釋循環打印出簡單的內容。下劃線模板通過_.each顯示對象陣列

<% _.each([comments], function(i) { %> <p><%= i %></p> <% }); %> 

打印的翻譯:

<% _.each([comments], function(i) { %> <p><%= JSON.stringify(i) %></p> <% }); %> 

打印:

[{"comment":"Mauris quis leo at diam molestie sagittis.","id":263,"observation_id":25}] 

我試過到目前爲止:

<% _.each([comments], function(i) { %> <p><%= i.comment %></p> <% }); %> 

空白

<% _.each([comments], function(i) { %> <p><%= i.get('comment') %></p> <% }); %> 

遺漏的類型錯誤:對象[對象數組]有沒有方法 '得到'

<% _.each([comments], function(i) { %> <p><%= comment %></p> <% }); %> 

空白

+0

什麼呢'評論'包含?這基本上是重要的。 – Loamhoof

+0

你爲什麼要在數組中包裝'comments'?我會假設它已經是一個數組了,對吧? – idbehold

回答

14

假定評論是在模型的數組:

<% _.each(comments, function(comment) { %> 
    <p><%= comment.comment %></p> 
<% }); %>