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
3 Answers
Reset to default 5You 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
评论列表(0条)