簡體   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