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