Javascript 是否可以将把手表达式值传递给子表达式?
如果父上下文可以访问属性索引,我可以将其传递到子表达式中吗?我似乎无法让它工作。我已经确认,with block使用传递给它的静态值1工作Javascript 是否可以将把手表达式值传递给子表达式?,javascript,html,handlebars.js,jsreport,Javascript,Html,Handlebars.js,Jsreport,如果父上下文可以访问属性索引,我可以将其传递到子表达式中吗?我似乎无法让它工作。我已经确认,with block使用传递给它的静态值1工作 // Parent template ----------------------------------------------------------------------------- {#child {{../report.ResourceName}}--{{Name}} @data.parentIndex={{@index}} } // Ch
// Parent template
-----------------------------------------------------------------------------
{#child {{../report.ResourceName}}--{{Name}} @data.parentIndex={{@index}} }
// Child template
-----------------------------------------------------------------------------
{{parentIndex}} // displays correctly as 2
// does not render
{{#with (lookup report.sections parentIndex)}}
Rendered: {{name}}
{{/with}}
// renders correctly
{{#with (lookup report.sections 2)}}
Rendered: {{name}}
{{/with}}
当您在子模板调用中使用动态数据(如每次迭代中的索引)时,您必须小心,因为事情可能不像您预期的那样工作 例如,当使用像这样的东西时
{{#each report.sections}}
{#child child @data.parentIndex={{@index}} }
<br />
{{/each}}
父模板的帮助程序:
{{#each report.sections}}
{{{callChild @index}}}
<br />
{{/each}}
function callChild(parentIndex) {
return '{#child child @data.parentIndex=' + parentIndex + '}'
}
{{parentIndex}}
{{#with (lookup report.sections parentIndex)}}
Rendered: {{name}}
{{/with}}
子模板:
{{#each report.sections}}
{{{callChild @index}}}
<br />
{{/each}}
function callChild(parentIndex) {
return '{#child child @data.parentIndex=' + parentIndex + '}'
}
{{parentIndex}}
{{#with (lookup report.sections parentIndex)}}
Rendered: {{name}}
{{/with}}
之所以这样做,是因为我们避免了Handlebar被括号语法弄糊涂,我们通过使用一个helper动态构造子调用来做到这一点,该helper最终在Handlebar处理模板后得到解决
最后是一个活生生的例子
希望对您有所帮助。先生,您真是太棒了。我不想因为我缺乏车把知识而打扰你们。我很感激。我有一个类似的解决方案,但我必须有一个地方,导致它无法工作的空间。这是我在别处找不到的很好的解释。