我确定我做错了很简单,但似乎找不到解释。我的模板中有以下行,它没有将任何值打印到 html 输出:
<%= _.each(@place.get("hash"),(count, tag) -> "#{tag} ") %>
此行将值打印到控制台非常好:
<%= _.each(@place.get("hash"),(count, tag) -> console.log "#{tag} ") %>
当我尝试使用打印命令并刷新时,谷歌浏览器会弹出一个打印菜单。我该如何解决这个问题
我确定我做错了很简单,但似乎找不到解释。我的模板中有以下行,它没有将任何值打印到 html 输出:
<%= _.each(@place.get("hash"),(count, tag) -> "#{tag} ") %>
此行将值打印到控制台非常好:
<%= _.each(@place.get("hash"),(count, tag) -> console.log "#{tag} ") %>
当我尝试使用打印命令并刷新时,谷歌浏览器会弹出一个打印菜单。我该如何解决这个问题
除了有用的 Underscore 方法 mu is too short提到之外,你还可以for of在 Eco 中使用 CoffeeScript 的 native:
<% for tag of @place.get("hash"): %>
<%= tag %>
<% end %>
如果您需要在每个元素周围添加一些标记,这可能很有用。例如:
<ul>
<% for tag of @place.get("hash"): %>
<li><%= tag %></li>
<% end %>
</ul>
下划线each不返回任何东西,所以<%= _.each(...) %>没有做任何有用的事情。你可以使用_.mapand join:
<%= _(@place.get('hash')).map((count, tag) -> tag).join(' ') %>
<%= _(@place.get('hash')).keys().join(' ') %>
您_.each只是在提取密钥,因此您应该说出您的意思。
如果您使用的是 node.s,那么您也应该拥有Object.keys:
<%= Object.keys(@place.get('hash')).join() %>
这是一个使用下划线方法迭代哈希的示例_.each:
给定哈希:
articlesByMonth = {'2014-07': [{id: 1, title: 'foo'}, {id: 2, title: 'bar'}]}
结合模板:
<ul>
<% _.each articlesByMonth, (articles, month) =>: %>
<%= month %>
<ul>
<% _.each articles, (article) =>: %>
<li><%= article.title %></li>
<% end %>
</ul>
<% end %>
</ul>