3

我有如下 JSON 数组:

{outer:[
    {
      Key1:"ID001", 
      Key2:[
             {
               innerKey1:"Myval1",
               innerKey2:"Myvalue2"
             }
            ]
    }
]}

现在在我的 HTML 文件中:

<div>
    {{#each outer}}
        <b>key1:</b> {{this.Key1}} <br/>
        {{#each this.Key2}}
           <b>InnerKey1:</b> {{this.innerKey1}} <br/>
        {{/each}}
    {{/each}}
</div>

但它没有显示任何内在价值。谁能帮助如何循环内部数组对象(即Key2上面)。我需要为此编写一个单独的助手吗?

4

1 回答 1

8

您必须在对象或数组交互之间进行选择:

{
  outer:[{
    Key1:"ID001", 
    Key2:{
      innerKey1:"Myvalue1",
      innerKey2:"Myvalue2"
    },
    Key3:[
      "Myvalue4",
      "Myvalue5"
    ]    
  }]
}

<div>
  {{#each outer}}
    key1: {{this.Key1}} <br/>
    {{#each this.Key2}}
      {{@key}}: {{this}}
    {{/each}} <br/>
    {{#each this.Key3}}
      {{@index}}: {{this}}
    {{/each}}
  {{/each}}
</div> 

输出:

key1: ID001 
innerKey1: Myvalue1 innerKey2: Myvalue2 
0: Myvalue4 1: Myvalue5

http://codepen.io/rafaelcastrocouto/pen/qgrFE

于 2013-08-16T13:40:37.957 回答