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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16070685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:09:03  来源:igfitidea点击:

Underscore template display array of objects via _.each

javascriptbackbone.jsunderscore.js

提问by ObviousCat

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

我无法使用 _.template 为每个评论循环打印一个简单的内容。

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

prints [object Object]

打印 [object Object]

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

prints:

印刷:

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

What I've tried so far:

到目前为止我尝试过的:

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

blank

空白的

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

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

未捕获的类型错误:对象 [对象数组] 没有方法“get”

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

blank

空白的

回答by Scott Puleo

Assuming comments is an array on your model:

假设评论是您模型上的一个数组:

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