【问题标题】:Handlebars access to nested propertiesHandlebars 访问嵌套属性
【发布时间】:2021-05-06 17:23:31
【问题描述】:

我正在尝试使用把手打印我从 json 获取的列表。数据结构很复杂(对我来说),所以我似乎无法找到我需要的嵌套属性。

数据如下:

var data = [{
    "response": {
        "status": "ok",
        "indexes": {
            "child": [
                {
                    "isDir": false,
                    "contentType": "video/avi",
                    "suffix": "avi",
                },
                {
                    "isDir": false,
                    "contentType": "video/avi",
                    "suffix": "avi",
                }
            ],
            "index": [
                {
                    "name": "A",
                    "dataIneed": [
                        {
                            "id": 291,
                            "name": "Example1"
                        },
                        {
                            "id": 292,
                            "name": "Example2"
                        },
                        {
                            "id": 293,
                            "name": "Example3"
                        },
                    ]
                }
            ],
            "lastModified": 1385842880050
        },
        "xmlns": "http://example.org/restapi",
        "version": "123"
    }
}]

在我要打印的表格中:

  • 示例1
  • 示例2
  • 示例3

我的 html / 车把看起来像这样:

<table class="table">
    {{#each }}
        <tr><td>
            {{response.indexes.index.0.dataIneed.0.name}}
        </td></tr>
    {{/each}}
</table>

并且能够打印:

  • 示例1

如何循环得到我想要的结果?

【问题讨论】:

    标签: javascript html json handlebars.js


    【解决方案1】:

    你必须指定循环的位置

    <table class="table">
      {{#each esponse.indexes.index.0.dataIneed}}
        <tr><td>
          {{this.name}}
        </td></tr>
      {{/each}}
    </table>
    

    【讨论】:

    • 谢谢,我想我找到了一种方法来做我想做的事。
    • @esdrayker 你发现了什么?
    猜你喜欢
    • 2019-06-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    • 2015-01-01
    • 2018-06-03
    • 2019-03-12
    • 1970-01-01
    • 2020-08-25
    相关资源
    最近更新 更多