如何在Handlebars模板中访问对象键?

8
我会把这个传递到我的模板中。
modules: {
  left: [],
  center: ['card', 'progressChart', 'tableOfChildren'],
  right: ['activity', 'details', 'triggers']
}

我希望能够做出类似于这样的事情...(伪代码,我认为不会起作用)
{{#each region in modules}}
  <div class="{{region}}">
  {{#each region}}
    <div class="{{this}} module"></div>
  {{/each}}
  </div>
{{/each}}
2个回答

17

8

可能是重复的问题,其中Jon提供了最佳解决方案:

对于数组:

{{#each myArray}}
    Index: {{@index}} Value = {{this}}
{{/each}}

对于对象:

{{#each myObject}}
    Key: {{@key}} Value = {{this}}
{{/each}}

请注意,只有通过hasOwnProperty测试的属性才会被枚举。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接