已经通过这个SO解决方案(以及更多)但无济于事:
How to iterate over array of objects in Handlebars?
鉴于此:
feeds: [
{
name: "Guardian",
data: "empty"
},
{
name: "TechCrunch",
data: [
{
"title": "Instagram is testing screenshot alerts for stories",
"author": "Fitz Tepper"
},
{
"title": "A group of industry insiders are putting Russian election meddling up for ad awards",
"author": "Jonathan Shieber"
},
{
"title": "The Trump administration is reportedly moving to privatize the International Space Station",
"author": "Jonathan Shieber"
}
]
},
{
name: "BBC",
data: "empty"
}
]
如何获取"标题"来自"数据"的信息阵列? "饲料" object被传递给render param中的.hbs。到目前为止,我已经能够提出以下内容,它在HTML / .hbs页面上显示为空白:
.hbs
<div class="tab-pane fade in" id="{{name}}" role="tabpanel">
<div id="results">
<script id="data-template" type="text/x-handlebars-template">
<div>
{{#data}}
{{#each this}}
{{title}}
{{/each}}
{{/data}}
</div>
</script>
</div>
</div>
<script>
$(function () {
let source = $("#data-template").html();
let template = Handlebars.compile(source);
let html = template({{contents}});
$('#results').html(html);
}
);
</script>
感谢您对此进行调查!
答案 0 :(得分:1)
替代回答
{{#each feeds}}
{{#each data}}
{{title}}
{{/each}}
{{/each}}
答案 1 :(得分:0)
由于title
和author
键位于feeds
数组的第2项内,因此您应该按以下方式引用该对象。
{{#each feeds.[1].data}}
{{title}}
{{/each}}