【问题标题】:How to get access to outside properties in a nested {{#each}}?如何访问嵌套 {{#each}} 中的外部属性?
【发布时间】:2015-11-26 05:22:20
【问题描述】:

我有一个{{#each}} 块,它贯穿司机必须进行的停靠列表。每个站点都需要有一个选择运行,并且选择从另一个集合中提取。所以我在另一个 {{#each}} 中有选择。

现在我需要以编程方式检查是否已经在数据库中为这个特定的停止选择了驱动程序,因为选择正在运行并被选中。

我的问题是我需要从外部{{#each}} 访问信息以在内部{{#each}} 中进行比较。

下面是我拥有的代码,但是当我运行它时,stopNumundefined

非常感谢这里的任何帮助。

<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>

【问题讨论】:

  • 尝试进入父作用域:../stopNum

标签: javascript meteor meteor-blaze spacebars


【解决方案1】:

#each 内,上下文设置为当前元素。

如果要获取外部项/属性,可以在模板代码中使用.. 或在帮助程序中使用Template.parentData(numLevels) 获取父上下文:

{{#each drivers}}
    {{#if dispatchDriverSelected driversName ../stopNum}}
        <option value="{{driversName}}" selected>{{driversName}}</option>
    {{else}}
        <option value="{{driversName}}">{{driversName}}</option>
    {{/if}}
{{/each}}

这是一个简单的例子:

模板结构:

<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>

助手:

Template.example.helpers({
  datum() {
    return {
      outer: 'outer 1',
      inner: [{
        prop: 'inner 1'
      },{
        prop: 'inner 2'
      }]
    }
  },
  someFunc(datum) {
    return `processed ${datum}`;
  }
});

呈现给:

<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>

【讨论】:

  • 非常感谢我不知道 ../ 这对我很有用。非常感谢。
猜你喜欢
  • 2012-11-21
  • 2014-02-18
  • 1970-01-01
  • 2019-12-13
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多