【问题标题】:Access parent variable from nested block in JsRender从 JsRender 中的嵌套块访问父变量
【发布时间】:2015-10-02 10:41:23
【问题描述】:

如何从嵌套的for 访问propskey

{{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 nested-loops parent jsrender


    【解决方案1】:

    这里有三种替代方法:

    提供key 作为上下文模板变量,所以它在{{for}} 块中可用:

    {{props object.items}}
        {{:key}}
        {{for prop.other_items ~outerKey=key}}
            Outer key: {{:~outerKey}}
       {{/for}}
    {{/props}}
    

    提供{{props}}块的数据项({key: ..., prop: ...}对象)作为上下文模板变量,所以它在{{for}}块中可用:

    {{props object.items itemVar="~outerProp"}}
        {{:key}}
        {{for prop.other_items}}
            Outer key: {{:~outerProp.key}}
        {{/for}}
    {{/props}}
    

    逐步遍历父视图(数组视图,然后是道具项目视图)并获取数据项({key: ..., prop: ...} 对象):

    {{props object.items}}
        {{:key}}
        {{for prop.other_items}}
            Outer key: {{:#parent.parent.data.key}}
        {{/for}}
    {{/props}}
    

    这里是 Matias 上一个问题的相关回复的链接:https://stackoverflow.com/a/31362057/1054484

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 2015-06-11
      相关资源
      最近更新 更多