Meteor 和 handlebars #each 迭代对象 [英] Meteor and handlebars #each to iterate over object
本文介绍了Meteor 和 handlebars #each 迭代对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将车把 #each
与不是数组的对象一起使用.
我该怎么做?我需要它仍然使用 #each
来处理流星的特殊功能.
我的对象是这样的:
<代码>{约翰:你好",鲍勃:你好"}
我正在尝试获得这样的输出:
你好<div>你好</div>
解决方案
你需要在你的js中使用一个helper来帮助handlebars理解你的对象:
添加到你的客户端js
Template.registerHelper('arrayify',function(obj){var 结果 = [];for (var key in obj) result.push({name:key,value:obj[key]});返回结果;});
并在您的 html 中使用(您也可以使用带有 {{name}}
的密钥):
{{#each arrayify myobject}}<div title="hover here {{name}}">{{value}}</div>{{/每个}}
myobject
来自您的模板:
Template.templatename.helpers({我的对象:函数(){返回{约翰:你好",鲍勃:你好"}}});
I want to use handlebars #each
with an object that's not an array.
How do I do that? I need it to still work with meteor's special features with #each
.
My object is in the form of:
{
john: "hello",
bob: "hi there"
}
I'm trying to get an output like this:
<div>hello</div>
<div>hi there</div>
解决方案
You need to use a helper in your js to help handlebars understand your object:
Add to your client js
Template.registerHelper('arrayify',function(obj){
var result = [];
for (var key in obj) result.push({name:key,value:obj[key]});
return result;
});
And use (you can also use the key with {{name}}
) in your html:
{{#each arrayify myobject}}
<div title="hover here {{name}}">{{value}}</div>
{{/each}}
myobject
comes from your template:
Template.templatename.helpers({
myobject : function() {
return { john:"hello", bob: "hi there" }
}
});
这篇关于Meteor 和 handlebars #each 迭代对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文