我有来自助手的 JSON
{
"Name": "abc",
"Age": 24,
"Address" {
"street" : "xyz street",
"city" : "zyz city",
"country" : "XY"
}
}
我想打印带有键和值的地址
<template name="User">
{{#with user}}
Name : {{Name}}
Age : {{Age}}
{{#each Address}}
{{key}} : {{value}} //Here is my question
{{/each}}
{{/with}}
</template>
如何打印模板中的键和值?
The {{#each}}
块助手仅接受游标和数组参数。
您可以重写地址助手以使其返回数组而不是对象。
Template.User.helpers({
Address: function(){
return _.map(this.Address, function(value, key){
return {
key: key,
value: value
};
});
}
});
您可能想将此实用程序函数定义为模板助手:
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)