【问题标题】:Meteor looses data context when adding a parameter to a subtemplate向子模板添加参数时 Meteor 丢失数据上下文
【发布时间】:2014-06-14 21:16:24
【问题描述】:

我有这个页面:

<template name="participant">
      {{#with participant}}
        <div class="participant">
          <h3>{{fullname}}</h3>
            <dl>
                <dt>E-mail</dt>
                <dd>{{email}}</dd>
                <dt>Phone</dt>
                <dd>{{tel}}</dd>
                <dt>City</dt>
                <dd>{{zip}} {{city}}</dd>
                <dt>Creation time</dt>
                <dd>added {{created_on}}</dd>
            </dl>
            {{>quickfield}}
        </div>
      {{/with}}
</template>

<template name="quickfield">
    <input id="{{email}}" value="{{email}}" class="bound">
</template>

而且运行良好。

现在,如果我想向我的快速字段子模板添加一个参数并执行以下操作:

{{>quickfield name="foo"}}
<template name="quickfield">
    <input id="{{name}}" value="{{email}}" class="bound">
</template>

数据上下文丢失(之前,this = 参与者,之后,this=Window)。 {{email}} 不再呈现。

为什么会这样,解决方法是什么?

谢谢!

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    也许你可以访问父上下文:

    <template name="quickfield">
      <input id="{{name}}" value="{{../email}}" class="bound">
    </template>
    

    【讨论】:

    • 不过,它可能无法解决 this 问题。
    • 是的,这是一个很好的解决方法,但我需要访问父级的整个数据上下文,因为我在事件中使用它然后......
    • 不,因为这样做时我丢失了局部变量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    相关资源
    最近更新 更多