在每个循环的Handlebars.js范围外访问一个变量 [英] Access a variable outside the scope of a Handlebars.js each loop
问题描述
我有一个handlebars.js模板,就像这样:
{{externalValue}}
< select name =test>
{{#each myCollection}}
< option value ={{id}}> {{title}} {{externalValue}}< / option>
{{/每个}}
< / select>
这是生成的输出:
myExternalValue
< option value =1>第一个元素< / option>
< option value =2>第二个元素< / option>
< option value =3>第三个元素< / option>
< / select>
正如预期的那样,我可以访问 id
和 title
myCollection
的每个元素的字段来生成我的选择。在选择之外,我的 externalValue
变量已被正确打印(myExternalValue)。
不幸的是,文本, externalValue
值永远不会打印出来。
我的问题是:如何访问范围之外的变量的handlebars.js每个从循环内?
与往常一样,在此先感谢您。
< option value ={{id}}> {{title}} {{../externalValue}}< ; /选项>
../
路径段引用父模板作用域应该是你想要的。
I have a handlebars.js template, just like this:
{{externalValue}}
<select name="test">
{{#each myCollection}}
<option value="{{id}}">{{title}} {{externalValue}}</option>
{{/each}}
</select>
And this is the generated output:
myExternalValue
<select name="test">
<option value="1">First element </option>
<option value="2">Second element </option>
<option value="3">Third element </option>
</select>
As expected, I can access the id
and title
fields of every element of myCollection
to generate my select. And outside the select, my externalValue
variable is correctly printed ("myExternalValue").
Unfortunately, in options' texts, externalValue
value is never printed out.
My question is: how can I access a variable outside the scope of the handlebars.js each from within the loop?
As always, thanks in advance.
Try
<option value="{{id}}">{{title}} {{../externalValue}}</option>
The ../
path segment references the parent template scope that should be what you want.
这篇关于在每个循环的Handlebars.js范围外访问一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!