[英]How Do I Access an Object's Properties From a Template?
According to http://handlebarsjs.com/expressions.html , I should be able to do this: 根据http://handlebarsjs.com/expressions.html ,我应该可以这样做:
<h1>{{article.title}}</h1>
But I can't seem to get this to work in meteor. 但我似乎无法让它在流星中发挥作用。 Here's my template:
这是我的模板:
<template name="content">
{{#if item}}
<p>{{item.name}}</p>
{{/if}}
</template>
Here's the JavaScript that returns the item: 这是返回项目的JavaScript:
Template.content.item = function() {
return Items.findOne({ _id: Session.get("list_id") });
};
And yes, the item does indeed have a property called name
:-) 是的,该项确实有一个名为
name
的属性:-)
When I do this, I see an error in Firebug that says ret is undefined
当我这样做时,我在Firebug中看到一个错误,表示
ret is undefined
This can be tracked down to evaluate.js: 这可以追溯到evaluate.js:
for (var i = 1; i < id.length; i++)
// XXX error (and/or unknown key) handling
ret = ret[id[i]];
return ret;
At the moment of the error, ret
references the window
object. 在错误发生时,
ret
引用window
对象。 What's up with that? 那是怎么回事?
You should use {{#with object}} 你应该使用{{#with object}}
If your object is something like : 如果您的对象是这样的:
my_object = {
name : 'my_name',
prop : 'my_prop'
}
In your template your can do : 在您的模板中,您可以:
<template name="my_template">
{{#with my_object}}
<p>Name is {{name}}<p>
<p>Prop is {{prop}}</p>
{{/with}}
</template>
Here you go :) 干得好 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.