javascript - Handlebars 在嵌套循环中访问外部索引

标签 javascript express handlebars.js express-handlebars

假设我有以下代码:

<div>
  {{#each questions}}
  <div id="question_{{@index}}">
    {{#each this.answers}}
    <div id="answer_{{???}}_{{@index}}">
      {{this}}
    </div>
    {{/each}}
  </div>
  {{/each}
</div>

如何访问内部(答案)循环内的外部循环索引(问题索引)?本质上我想要 id 格式为 "answer_questionIndex_answerIndex"

最佳答案

在一些文档中发现了这一点

Block Parameters

New in Handlebars 3.0, it's possible to receive named parameters from supporting helpers.

{{#each users as |user userId|}}
  Id: {{userId}} Name: {{user.name}}
{{/each}}

In this particular example, user will have the same value as the current context and userId will have the index value for the iteration.

https://handlebarsjs.com/guide/block-helpers.html#hash-arguments

关于javascript - Handlebars 在嵌套循环中访问外部索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59411663/

相关文章:

javascript - 在 Node.js 中的函数和 JavaScript 文件之间隐式传递上下文

javascript - antlr 3.4 或 3.5 中 Javascript 的状态

javascript - 在 Node-RED 中,使用 npm 安装后如何引用 javascript 库

node.js - 如何在 expressjs 中将 session 数据存储在文件中?

ember.js - Ember JS : Could not find property 'action'

javascript - Ember Js 1.0,计算属性和模板在数据更改时不刷新

javascript - 如何在Meteor模板中通过ID访问另一个集合?

javascript - angularjs ui.router 抛出错误 : Unexpected request: GET

javascript - 使用 fullPage.js 时无法单击 Bootstrap 模式对话框

javascript - 在express应用程序中异步执行res.render