【问题标题】:Get {{#index}} parent of the first for loop inside the second for loop in JSrender在 JSrender 中的第二个 for 循环中获取第一个 for 循环的 {{#index}} 父级
【发布时间】:2013-11-12 11:14:33
【问题描述】:

我有两个 for 循环,第二个在第一个循环内,我想在第二个循环内获取第一个循环的 #index。

这是我的代码:

    {{for parentObject}}
      {{for childObject}}
           <input id="myId{{:#indexParent}}{{:#index}}"/>
      {{/for}}
    {{/for}}

我试过这段代码:

    {{for parentObject ~parent=#index}}
      {{for childObject}}
           <input id="myId{{:~parent}}{{:#index}}"/>
      {{/for}}
    {{/for}}

但它不起作用!

【问题讨论】:

    标签: html jsrender jsviews


    【解决方案1】:

    我假设您的对象是数组。 ~parent=#index 需要在“item”模板块上——({{for outerArray}} 的内容)。

    {{for parentObject}}
      {{for childObject  ~parent=#index}}
           <input id="myId{{:~parent}}{{:#index}}"/>
      {{/for}}
    {{/for}}
    

    您在上下文中选择的索引太高了。

    【讨论】:

    • 是的,我的对象是数组。所以我尝试了这个解决方案,但它不起作用!它什么也不显示。谢谢。
    • 如果什么都不显示,那么您的 childObject 或 parentObject 迭代不起作用。您应该首先使用“aaa{{for parentObject}}xxx{{for childObject}}yyy{{/for}}{{/for}}bbb”进行测试。那是输出“aaabbb”还是“aaaxxxyyy...bbb”还是什么?也许您的问题是“更高”。之后尝试类似:“aaa{{for parentObject}}{{:#index}}xxx{{for childObject}}{{:#index}}xxxyyy{{/for}}{{/for}}bbb”
    【解决方案2】:

    试试这个,

    {{for parentObject ~parent=#index}}
      {{for childObject}}
           <input id="myId{{:#parent.index}}"/>
      {{/for}}
    {{/for}}
    

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多