获取车把中父数组的索引
Get index of parent array in handlebars
我有一组问题数据,每个问题中都有一组可选答案。我要做的是获取问题的索引和每个答案的答案。
所以我让 handlebars 循环遍历问题,然后循环遍历每个问题的答案。这是:
{{#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: Parse error on line 16:
... {{../@index}} {{this}}
Error: /home/ubuntu/workspace/views/survey/index.hbs: Parse error on
line 16: ... {{../@index}} {{this}}
这是一个显示它不起作用的示例:https://codepen.io/samkeddy/pen/oeMeeO(如果你取出 {{../@index}} 它会起作用。
尝试{{@../index}}-{{@index}}={{this}}
我有一组问题数据,每个问题中都有一组可选答案。我要做的是获取问题的索引和每个答案的答案。
所以我让 handlebars 循环遍历问题,然后循环遍历每个问题的答案。这是:
{{#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: Parse error on line 16: ... {{../@index}} {{this}}
Error: /home/ubuntu/workspace/views/survey/index.hbs: Parse error on line 16: ... {{../@index}} {{this}}
这是一个显示它不起作用的示例:https://codepen.io/samkeddy/pen/oeMeeO(如果你取出 {{../@index}} 它会起作用。
尝试{{@../index}}-{{@index}}={{this}}