如何使用Handlebars.js从JSON访问内部对象?

时间:2017-09-21 12:43:31

标签: javascript handlebars.js

我正在尝试使用Handlebars.js从JSON访问内部对象。我试过,但我没有得到地址对象有区域值。

这是我的HTML:

<table class="table table-striped">
  <thead>
    <tr>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Email</th>
      <td>Address</td>
    </tr>
  </thead>
  <tbody>   
    {{#empdetails}}
    <tr> 
      <td>{{firstname}}</td>
      <td>{{lastname}}</td>
      <td>{{email}}</td>
      {{#each addres}}
      <td>{{addres.area}}</td>
      {{#each}}
    </tr>
  {{/empdetails}}
  </tbody>
</table>

这是我的JavaScript:

var data = {empdetails: [
  {firstname: "Success", lastname: "Doe", email: "john@example.com",addres:[{area:"mtm"}]},
   {firstname: "Danger", lastname: "Moe", email: "mary@example.com",addres:[{area:"vij"}]},
  {firstname: "ravi", lastname: "m", email: "ramu@example.com",addres:[{area:"chennai"}]},
  {firstname: "sandeep", lastname: "sand", email: "sandeep@example.com",addres:[{area:"mumbai"}]},
  {firstname: "kishore", lastname: "kis", email: "kishore@example.com",addres:[{area:"delhi"}]},
  {firstname: "vivek", lastname: "ve", email: "vivek@example.com",addres:[{area:"vijag"}]},
  {firstname: "adi", lastname: "udathu", email: "adi@example.com",addres:[{area:"guntur"}]},
  {firstname: "prem", lastname: "muk", email: "prem@example.com",addres:[{area:"nellore"}]},
  {firstname: "ramesh", lastname: "Doe", email: "ramesh@example.com",addres:[{area:"mtm"}]},
  {firstname: "naresh", lastname: "muk", email: "naresh@example.com",addres:[{area:"rajamandri"}]}
]};

请帮助我获得address->area值。

1 个答案:

答案 0 :(得分:1)

使用{{this.area}}。每个块内的this对应于循环中当前索引处的项目。

Under the "The each block helper" section

  

在块中,您可以使用this来引用正在迭代的元素。