在jsrender中访问循环内的父项

问题描述 投票:7回答:1
<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。

jsrender
1个回答
13
投票
您可以引入在循环内可见的变量(在官方文档中: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}}"...

© www.soinside.com 2019 - 2024. All rights reserved.