我正在尝试使用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: {
}
});
})();发布于 2016-02-10 23:04:50
这实际上很容易做到。在下面找到代码:
<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”获取子数组。
https://stackoverflow.com/questions/35316519
复制相似问题