假设我有以下代码:
<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/