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

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

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);
 });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745398093a4625972.html

相关推荐

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

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

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信