问题描述
如何从嵌套的for
访问props
的key
?
{{props object.items}}
{{:key}}
{{for prop.other_items}}
{{:key}} //here I want to print the key from props
我尝试过:
{{:key}}
{{:#key}}
{{:#parent.key}}
{{:#parent.parent.key}}
{{:~root.key}}
推荐答案
以下是三种替代方法:
提供key
作为上下文模板变量,因此在{{for}}
块中可用:
Provide the key
as a contextual template variable, so it is available in the {{for}}
block:
{{props object.items}}
{{:key}}
{{for prop.other_items ~outerKey=key}}
Outer key: {{:~outerKey}}
{{/for}}
{{/props}}
提供{{props}}
块({key: ..., prop: ...}
对象)的数据项作为上下文模板变量,因此在{{for}}
块中可用:
Provide the data item of the {{props}}
block (the {key: ..., prop: ...}
object) as a contextual template variable, so it is available in the {{for}}
block:
{{props object.items itemVar="~outerProp"}}
{{:key}}
{{for prop.other_items}}
Outer key: {{:~outerProp.key}}
{{/for}}
{{/props}}
逐步浏览父视图(数组视图,然后显示道具项目视图)并获取数据项({key: ..., prop: ...}
对象):
Step up through the parent views (array view, then props item view) and get the data item (the {key: ..., prop: ...}
object):
{{props object.items}}
{{:key}}
{{for prop.other_items}}
Outer key: {{:#parent.parent.data.key}}
{{/for}}
{{/props}}
这是对Matias先前问题的相关答复的链接: https://stackoverflow.com/a/31362057 /1054484
And here is a link to a related reply to a previous question from Matias: https://stackoverflow.com/a/31362057/1054484
这篇关于从JsRender中的嵌套块访问父变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!