如何迭代Handlebars中的对象数组? [英] How to iterate over array of objects in Handlebars?

查看:561
本文介绍了如何迭代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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆