首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Polymer iron-ajax检索JSON子数组

使用Polymer iron-ajax检索JSON子数组
EN

Stack Overflow用户
提问于 2016-02-10 21:25:11
回答 1查看 841关注 0票数 0

我正在尝试使用iron-ajax检索一个JSON文件,以便创建一个导航折叠菜单。到目前为止,这在“顶级”标题中读得很好,但我正在努力返回子菜单名称。

我想知道我是否需要在nav-head元素中嵌套一个模板dom-repeat,但到目前为止还没有成功。

JSON示例:

代码语言:javascript
复制
{
 "headers": [
  {
   "name": "Header One",
    "sub": [
     {
      "name": "Sub Heading One"
     },
     {
      "name": "Sub Heading Two"
     }
    ]
   }
  ]
 }

聚合物代码:

代码语言:javascript
复制
<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: {
    }

  });
})();
EN

回答 1

Stack Overflow用户

发布于 2016-02-10 23:04:50

这实际上很容易做到。在下面找到代码:

代码语言:javascript
复制
<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”获取子数组。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35316519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档