【问题标题】:I lose data context when i generate template with parameters当我生成带有参数的模板时,我丢失了数据上下文
【发布时间】:2015-09-16 09:36:45
【问题描述】:

当我在#each 助手中生成子模板并添加参数时,我会丢失数据上下文,通常是可见的。

我通过将数据字段传递给模板找到了解决方法

{{> productItem parameter="test" name=name details=details}}

,但是对于更复杂的集合来说会很烦人......没有更好的选择来解决这个问题吗?

<template name="main">
    {{#each products}}
        {{> productItem parameter="test"}}
    {{/each}}
</template>

<template name="productItem">
    <div class="product">
        <p>{{name}}</p>
        <p>{{details}}</p>
        <p>{{parameter}}</p>
    </div>
</template>

还有 javascript:

Template.main.helpers({
    products: function(){
        return Products.find({});
    }
});

【问题讨论】:

    标签: meteor handlebars.js


    【解决方案1】:

    您正在创建一个新的上下文(它不会神奇地合并所有内容),但它很容易包含原始上下文。

    你去:-

     {{> productItem product=this parameter="test"}}
    

    然后

    <template name="productItem">
        <div class="product">
            <p>{{product.name}}</p>
            <p>{{product.details}}</p>
            <p>{{parameter}}</p>
        </div>
    </template>
    

    <template name="productItem">
        <div class="product">
           {{#with product}}
            <p>{{name}}</p>
            <p>{{details}}</p>
           {{/with}}
            <p>{{parameter}}</p>
        </div>
    </template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2023-03-12
      • 1970-01-01
      • 2011-07-15
      • 2018-07-26
      • 2015-05-24
      • 1970-01-01
      相关资源
      最近更新 更多