最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Underscore template display array of objects via _.each - Stack Overflow

programmeradmin3浏览0评论

I'm having trouble printing out a simple for each ment loop with _.template.

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

prints [object Object]

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

prints:

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

What I've tried so far:

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

blank

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

Uncaught TypeError: Object [object Array] has no method 'get'

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

blank

I'm having trouble printing out a simple for each ment loop with _.template.

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

prints [object Object]

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

prints:

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

What I've tried so far:

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

blank

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

Uncaught TypeError: Object [object Array] has no method 'get'

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

blank

Share Improve this question asked Apr 17, 2013 at 21:36 ObviousCatObviousCat 5051 gold badge5 silver badges17 bronze badges 2
  • And what does ments contains? That's basically what's important here. – Loamhoof Commented Apr 17, 2013 at 21:45
  • And why are you wrapping ments in an array? I would assume that it's already an array, right? – idbehold Commented Apr 17, 2013 at 22:04
Add a ment  | 

1 Answer 1

Reset to default 15

Assuming ments is an array on your model:

<% _.each(ments, function(ment) { %>  
  <p><%= ment.ment %></p> 
<% }); %>
发布评论

评论列表(0)

  1. 暂无评论