繁体   English   中英

如何在Meteor Template中打印键和值?

[英]How to print key and values in Meteor Template?

我有助手的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>

如何在模板中打印键和值?

{{#each}}块帮助程序只接受游标和数组参数。

您可以覆盖Address帮助程序,使其返回数组而不是对象。

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>

要在JS中进行更改

var AddressSet=CollectionName.find( {  } );

要在HTML中进行更改

      {{#each AddressSet}}
        {{#each Address}}
              {{this.street}}
              {{this.city}}
              {{this.country}}
       {{/each}}

       {{/each}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM