我想使用下划线模板显示 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"
}
]
}
}
];
我正在读取对象并将其绑定到这样的模板,它使用这个
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),但我仍然需要弄清楚如何循环它并显示。有任何想法吗?