【发布时间】:2023-03-15 18:33:01
【问题描述】:
我有一组问题的数据,每个问题里面都有一组可选答案。我要做的是获取问题的索引和每个答案的答案。
所以我让车把遍历问题,然后遍历每个问题的答案。就是这样:
{{#questions}}
<ul>
<li><strong>{{question}}</strong><br>
{{#each answers}}
{{@index}}={{this}},
{{/each}}</li>
</ul>
{{/questions}}
这是输入的数据:
data = {
"questions" : [
{
"question":"Favorite Color",
"answers" : ["red","blue","green"]
},
{
"question":"Favorite food",
"answers" : ["pizza","pasta","cats"]
}
]
}
所以如果我想要答案的索引,我可以写@index,但如果它是一个数组内的数组,它就不起作用。您应该可以编写 ../ 从父级获取变量,但数组不喜欢那样。
{{../@index}}-{{@index}}={{this}}
这应该给我 questionIndex-answerIndex=answer。
但我收到一个错误,如下所示:
/home/ubuntu/workspace/views/survey/index.hbs:第 16 行解析错误: ... {{../@index}} {{this}}
错误:/home/ubuntu/workspace/views/survey/index.hbs:解析错误 第 16 行:... {{../@index}} {{this}}
这是一个显示它不起作用的示例:https://codepen.io/samkeddy/pen/oeMeeO(如果你取出 {{../@index}} 它会起作用。
【问题讨论】:
标签: javascript arrays handlebars.js mustache