如何迭代Handlebars中的对象数组? [英] How to iterate over array of objects in Handlebars?
问题描述
我碰到这个Web Api,它返回一个对象数组, JSON格式:
>
Handlebars文档显示以下示例:
< ul class =people_list >
{{#each people}}
< li> {{this}}< / li>
{{/ each}}
< / ul>
在上下文中:
{
people:[
Yehuda Katz,
Alan Johnson,
Charles Jolley
]
}
在我的情况下,我没有数组的名称,它只是根对象的回应。我已经尝试过使用 {{#each}}
,但没有运气。首次使用Handlebars ...我缺少什么?
更新
$ b 这里有一个简单的小提示,我问: http://jsfiddle.net/KPCh4/2/
是否handlebars要求上下文变量是一个对象而不是数组?
您可以将这个
给每个块。看到这里: http://jsfiddle.net/yR7TZ/1/
{{#each this}}
< div class =row>< / div> ;
{{/ each}}
This might seem a silly question but I can't seem to find the answer anywhere.
I'm hitting this Web Api that returns an array of objects in JSON format:
Handlebars docs shows the following example:
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
in the context of:
{
people: [
"Yehuda Katz",
"Alan Johnson",
"Charles Jolley"
]
}
In my case I don't have a name for the array, it's just the root object of the response. I've tried using {{#each}}
with no luck.
First time using Handlebars... what am I missing?
UPDATE
Here's a simplified fiddle to show what I'm asking: http://jsfiddle.net/KPCh4/2/
Does handlebars require the context variable to be an object and not an array?
You can pass this
to each block. See here: http://jsfiddle.net/yR7TZ/1/
{{#each this}}
<div class="row"></div>
{{/each}}
这篇关于如何迭代Handlebars中的对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!