繁体   English   中英

下划线模板通过_.each显示对象数组

[英]Underscore template display array of objects via _.each

我在使用_.template打印每个注释循环的简单时遇到问题。

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

打印[object Object]

<% _.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> <% }); %>

未捕获的TypeError:对象[object Array]没有方法'get'

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

空白

假设注释是模型上的数组:

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

暂无
暂无

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

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