【问题标题】:Access parent item inside a loop in jsrender访问jsrender循环内的父项
【发布时间】:2012-12-17 16:40:24
【问题描述】:
<select id="Test" TestAttr="{{:LocationId}}">              
       {{for #parent.parent.data.Location}}                         
                  <option value="{{:LocationId}}" {{if LocationId= *#parent.parent.data.LocationId*}}selected{{/if}}>{{:#parent.parent.data.LocationId}}</option>                            
       {{/for}}
</select>

如何在**之间提到的if语句中获取父数组的LocationId。

【问题讨论】:

  • 如果我在选项标签中给出 {{:#parent.parent.data.LocationId}} 我能够看到它的价值。但我想在 if 语句中使用相同的内容。
  • Location 是具有要创建下拉列表的值的子数组。

标签: jsrender


【解决方案1】:

您可以引入在循环内可见的变量 (在官方文档中:Setting contextual template parameters, accessible in all nested contexts as ~nameOfParameter

<select id="Test" TestAttr="{{:LocationId}}">  
    {{for #parent.parent.data.Location ~locationId=LocationId}}
       <option value="{{:~locationId}}"...


已编辑后记:

以上链接指向以前的文档,现已被取代。

有关当前文档,请参阅Accessing parent data

另请参阅stackoverflow question

【讨论】:

  • 感谢您的回复。如何在 if 语句中使用它?
  • {{if LocationId == ~locationId }}
猜你喜欢
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
相关资源
最近更新 更多