admin管理员组文章数量:1323330
I have JSON from helper
{
"Name": "abc",
"Age": 24,
"Address" {
"street" : "xyz street",
"city" : "zyz city",
"country" : "XY"
}
}
I want to print the address with key and values
<template name="User">
{{#with user}}
Name : {{Name}}
Age : {{Age}}
{{#each Address}}
{{key}} : {{value}} //Here is my question
{{/each}}
{{/with}}
</template>
How to print key and values in a template?
I have JSON from helper
{
"Name": "abc",
"Age": 24,
"Address" {
"street" : "xyz street",
"city" : "zyz city",
"country" : "XY"
}
}
I want to print the address with key and values
<template name="User">
{{#with user}}
Name : {{Name}}
Age : {{Age}}
{{#each Address}}
{{key}} : {{value}} //Here is my question
{{/each}}
{{/with}}
</template>
How to print key and values in a template?
Share edited May 14, 2015 at 10:23 Ramesh Murugesan asked May 14, 2015 at 10:15 Ramesh MurugesanRamesh Murugesan 5,0238 gold badges43 silver badges71 bronze badges2 Answers
Reset to default 7The {{#each}}
block helper only accepts cursors and arrays arguments.
You could override the Address helper to make it return an array instead of an object.
Template.User.helpers({
Address: function(){
return _.map(this.Address, function(value, key){
return {
key: key,
value: value
};
});
}
});
You might want to define this utility function as a template helper :
JS
Template.registerHelper("objectToPairs",function(object){
return _.map(object, function(value, key) {
return {
key: key,
value: value
};
});
});
HTML
<template name="User">
<ul>
{{#each objectToPairs Address}}
<li>{{key}} - {{value}}</li>
{{/each}}
</ul>
</template>
Changes to be made in JS
var AddressSet=CollectionName.find( { } );
Changes to be made in HTML
{{#each AddressSet}}
{{#each Address}}
{{this.street}}
{{this.city}}
{{this.country}}
{{/each}}
{{/each}}
本文标签: javascriptHow to print key and values in Meteor TemplateStack Overflow
版权声明:本文标题:javascript - How to print key and values in Meteor Template? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742138435a2422473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论