[英]How to get access to outside properties in a nested {{#each}}?
I have an {{#each}}
block that runs through a list of stops that a driver must make. 我有一个
{{#each}}
块,该块贯穿了驾驶员必须进行的停车的清单。 Each stop needs to have a select run and the select draws in from another collection. 每个停靠点都需要进行选择运行,选择从另一个集合中吸取。 So I have the select in another
{{#each}}
. 因此,我在另一个
{{#each}}
进行了选择。
Now I need to programmatically check if a driver was already selected in the db for this particular stop as the select is running and have it get selected. 现在,我需要以编程方式检查是否已在数据库中为此特定停止选择了驱动程序,因为选择正在运行并被选中。
My problem is that I need to access info from the external {{#each}}
to do the comparison in the internal {{#each}}
. 我的问题是我需要从外部
{{#each}}
访问信息以在内部{{#each}}
。
Below is the code that I have but when I run it the stopNum
is undefined
. 下面是我拥有的代码,但是当我运行它时,
stopNum
是undefined
。
Any help here is greatly appreciated. 非常感谢您的任何帮助。
<td class="text-left">
{{#if notEquals stopNum 0}}
<select class="form-control driverName clearForm" id="driverName{{stopNum}}" name="driverName{{stopNum}}">
<option value="" ></option>
{{#each drivers}}
{{#if dispatchDriverSelected driversName stopNum}}
<option value="{{driversName}}" selected>{{driversName}}</option>
{{else}}
<option value="{{driversName}}">{{driversName}}</option>
{{/if}}
{{/each}}
</select>
{{/if}}
</td>
Within #each
, the context is set to the current element. 在
#each
,上下文设置为当前元素。
If you want to get an external item/property, you can get the parent context using ..
in the template code or using Template.parentData(numLevels)
in helpers: 如果要获取外部项目/属性,则可以在模板代码中使用
..
或在助手中使用Template.parentData(numLevels)
来获取父上下文:
{{#each drivers}}
{{#if dispatchDriverSelected driversName ../stopNum}}
<option value="{{driversName}}" selected>{{driversName}}</option>
{{else}}
<option value="{{driversName}}">{{driversName}}</option>
{{/if}}
{{/each}}
Here is a simple example: 这是一个简单的示例:
Template structure: 模板结构:
<template name="example">
{{#with datum}}
<div class="wrapper">
{{outer}}
<div class="items">
{{#each inner}}
<div class="inner">
{{prop}} {{someFunc ../outer}}
</div>
{{/each}}
</div>
</div>
{{/with}}
</template>
Helpers: 帮手:
Template.example.helpers({
datum() {
return {
outer: 'outer 1',
inner: [{
prop: 'inner 1'
},{
prop: 'inner 2'
}]
}
},
someFunc(datum) {
return `processed ${datum}`;
}
});
renders to: 呈现给:
<div class="wrapper">
outer 1
<div class="items">
<div class="inner">
inner 1 processed outer 1
</div>
<div class="inner">
inner 2 processed outer 1
</div>
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.