最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get template variable in onCreated in Meteor - Stack Overflow

programmeradmin1浏览0评论

I am rendering a template with Blaze.renderWithData(Template.templateName, { key: value });

I can get the value in my template with {{key}}, but I cannot get the value in my js code.

I have tried

Template.templateName.onCreated( () => {
  console.log(Template.instance().key);
});

but the variable is undefined.

I am rendering a template with Blaze.renderWithData(Template.templateName, { key: value });

I can get the value in my template with {{key}}, but I cannot get the value in my js code.

I have tried

Template.templateName.onCreated( () => {
  console.log(Template.instance().key);
});

but the variable is undefined.

Share Improve this question asked Dec 28, 2015 at 15:20 JamgreenJamgreen 11.1k32 gold badges122 silver badges232 bronze badges 1
  • Have you tried using the onRendered callback instead of onCreated?docs.meteor./#/full/template_onCreated: "Callbacks added with this method are called before your template's logic is evaluated for the first time. Inside a callback, this is the new template instance object. Properties you set on this object will be visible from the callbacks added with onRendered and onDestroyed methods and from event handlers." – Jeremiah Commented Dec 28, 2015 at 18:34
Add a ment  | 

3 Answers 3

Reset to default 5

You can use

this.data.key

or

Template.instance().data.key

Cheers

It should be

Template.instance().data['your-key']

If you have doubt about what is the value, put the break on the source code of the chrome developer tools or firebug and try to debug. This is the client side, thus all the code will be available

Data passed to template is available on this in onCreated function, so this should works:

 Template.templateName.onCreated( () => {
      console.log(this.key);
 });
发布评论

评论列表(0)

  1. 暂无评论