在这里提琴:
在下划线模板内的这个 for 循环中:
<% for(var item in thing.items) { %>
我想得到 item.name,但<%= item.name %>
没有输出任何东西。
如何从每个things.items
对象中获取属性?谢谢!
我的 JSON 数据如下所示:
var things = [{
"name": "Chair",
"title": "Chairs",
"items": [{
"name": "Recliner",
"title": "Recliner Chair",
"type": "Chair",
"quantity": "1"
},
{
"name": "Club/Armchair",
"title": "Club/Armchair",
"type": "Chair",
"quantity": 1
}]
},
{
"name": "Table",
"title": "Tables",
"items": [{
"name": "End Table",
"title": "End Table",
"type": "Table",
"quantity": "1"
},
{
"name": "Coffee Table",
"title": "Coffee Table",
"type": "Table",
"quantity": 1
}]
}];
我的模板如下所示:
<script type="text/html" id='furniture-template'>
<% _.each(things,function(thing,key,list){
// create more variables
%>
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" href="#things-<%= thing.name %>">
<%= thing.title %>
</a>
</div> <!-- header -->
<div id="things-<%= thing.name %>" class="accordion-body collapse in">
<div class="accordion-inner">
<% for(var item in thing.items) { %>
<div class="item">
<a class="item-add" data-type="Chairs" data-name="Recliner"><%= item.name %></a>
</div>
<% } %>
</div> <!-- inner -->
</div> <!-- accordion-body -->
<% }); %>
</script>