【发布时间】:2015-06-24 11:01:02
【问题描述】:
我想使用下划线模板显示一个 JS 数据对象。我似乎无法弄清楚如何钻取对象以获取国家名称或其他日期(例如 tarrifType)并使用我的模板显示它。对象看起来像这样...
var items = [
{
"country": {
"China": [
{
"tarrifType": "China Pass",
"fixLine": "23p",
},
{
"tarrifType": "Monthly plan",
"fixLine": "3p",
}
],
"Australia": [
{
"tarrifType": "Pay as you go",
"fixLine": "73p"
},
{
"tarrifType": "Australia Pass",
"fixLine": "49p",
},
{
"tarrifType": "Monthly plan",
"fixLine": "20p",
}
],
"Nigeria": [
{
"tarrifType": "Pay as you go",
"fixLine": "73p"
},
{
"tarrifType": "Nigeria Pass",
"fixLine": "49p"
}
]
}
} ];
我正在读取对象并使用 this 将其绑定到这样的模板
var tableTemplate = $("#table-data").html();
$("table.outer tbody").html(_.template( tableTemplate, {items:items} ));
我正在使用这个下划线模板...
<script type="text/html" id='table-data'>
<% _.each(items,function(item,key,list){ %>
<tr>
<td></td>
<td><%- item.country %></td>
</tr>
<% }) %>
</script>
到目前为止,我没有收到任何错误,但模板渲染但只显示 [object Object],所以我认为它几乎就在那里。我尝试使用点表示法(item.country),但我仍然需要弄清楚如何循环并显示。有什么想法吗?
【问题讨论】:
标签: javascript templates underscore.js underscore.js-templating