簡體   English   中英

使用Polymer Iron-Ajax檢索JSON子數組

[英]Retrieving JSON sub-array using Polymer iron-ajax

我正在嘗試使用iron-ajax檢索JSON文件,以便創建導航手風琴菜單。 到目前為止,這在“頂級”標題中的讀取效果很好,但是我正在努力返回子菜單名稱。

我想知道是否需要在nav-head元素中使用嵌套模板dom-repeat,但到目前為止仍無法成功。

JSON樣本:

{
 "headers": [
  {
   "name": "Header One",
    "sub": [
     {
      "name": "Sub Heading One"
     },
     {
      "name": "Sub Heading Two"
     }
    ]
   }
  ]
 }

聚合物代碼:

<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="nav-head.html">
<link rel="import" href="nav-sub.html">

<dom-module id="nav-accordion">
  <template>
    <style include="shared-styles">
    </style>

<iron-ajax auto
      url="../../../api/nav.json"
      handle-as="json"
      last-response="{{ajaxResponse}}"></iron-ajax>

<template is="dom-repeat" items="[[ajaxResponse.headers]]">
  <div class="horizontal-section">
    <nav-head heading=[[item.name]]></nav-head>
  </div>
</template>

</template>

<script>
(function() {
  'use strict';

  Polymer({
    is: 'nav-accordion',

    properties: {
    }

  });
})();

這實際上很簡單。 在下面找到代碼:

<template is="dom-repeat" items="{{ajaxResponse.headers}}" as="header">
  <div class="horizontal-section">
    <nav-head heading={{header.name}}>
      <template is="dom-repeat" items="{{header.sub}}" as="sub">
        <nav-sub subheading={{sub.name}}></nav-sub>
      </template>
    </nav-head>
  </div>
</template>

要記住的重要事情是使用“ header.sub”而不是“ ajaxResponse.sub”來獲取子數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM