流星:在子模板中使用变量
Meteor: Using variable in child template
我正在使用模板级订阅,但是在子模板中使用结果时遇到了一些问题:
当我加载模板example
时,将显示模板exampleChild
(仅将此作为基本示例 - 我知道现在没有意义(。
在创建主模板时,订阅已完成,数据存储在帮助程序中:
模板
<template name="example">
{{> Template.dynamic template=switch}}
</template>
<template name="exampleChild">
<h1>{{result}}</h1>
</template>
助手
Template.example.helpers({
switch: function() { return 'exampleChild'; },
result: function() { return Template.instance().result(); },
});
事件
Template.example.onCreated(function() {
var instance = this;
instance.autorun(function () {
var subscription = instance.subscribe('posts');
});
instance.result = function() {
return Collection.findOne({ _id: Session.get('id') });
}
});
如果我将{{result}}
放在example
模板中,一切正常。但是我需要在子模板中使用变量。
你试过吗:
<template name="example">
{{#with result}}
{{> Template.dynamic template=switch}}
{{/with}}
</template>
<template name="exampleChild">
<h1>{{this}}</h1>
</template>
带文档
这样做时,我更喜欢将数据上下文结果包装到一个对象中,以便在子对象上有一些适当的内容,例如:
Template.example.helpers({
switch: function() { return 'exampleChild'; },
data: function() { return {result: Template.instance().result()} },
});
<template name="example">
{{#with data}}
{{> Template.dynamic template=switch}}
{{/with}}
</template>
<template name="exampleChild">
<h1>{{result}}</h1>
</template>
希望有帮助
您可以将结果存储在会话变量中,然后将另一个帮助程序添加到等待会话变量的exampleChild
。
相关文章:
- 我怎么能让流星简单模式中的一个字段等于一个js变量,并且仍然让用户填写简单模式的其余部分
- 如何处理流星变量&对miniMongo有问题
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- 流星-可以't在subscribe中传递变量
- 流星:如何避免检查到处是否存在变量或属性
- 流星 在客户端跟踪服务器端变量
- 流星函数与变量跟踪
- 流星:变量随机设置为 NaN
- 流星 - 从儿童助手访问父模板变量
- 流星:在子模板中使用变量
- 流星中的全局变量
- 有没有办法将变量传递到流星中的模板中
- 由单个模板变量多次调用的流星助手
- 流星:我可以在模板助手之外使用反应式变量吗?
- 当更新流星中的嵌套集合时,如何为更新路径传递变量
- 流星.js&空格键-在嵌套循环中传递变量
- 将变量传递给流星助手
- 流星JS.每个循环中的一个变量
- 流星如何导入全局变量
- 流星环境变量在生产环境中无效