我正在尝试在json数组内的json数组中使用dom-repeat内的dom-repeat。如何将第一个数组中的值传递给第二个dom-repeat?
为了说明,我有以下json数组加载正常:
"books": [
{
"title": "book1",
"slug": "b1",
"authors": ["author1","author2"],
"blurb": "Some text"
},
{
"title": "book2",
"slug": "b2",
"authors": ["author2","author3"],
"blurb": "some more text"
}]
我正在迭代数组'books'并检索title和blurb。然后在下面,我希望列出每本书的两位作者,并且我还希望以/ slug / author(f.i. / b1 / author1)的格式链接到每一本书。但是因为在第二个dom-repeat中我重新定义了“items”,所以'slug'不再可用。我该怎么做?
<template>
<iron-ajax
auto
url="somelist.json"
handle-as="json"
last-response="{{someList}}"></iron-ajax>
<template is="dom-repeat" items="{{someList.books}}">
<paper-collapse-group>
<paper-collapse-item header$="{{item.title}}">
<p>{{item.blurb}}</p>
</paper-collapse-item>
<template is="dom-repeat" items="{{item.authors}}">
<a href$="/[[slug]]/[[item]]">{{item}}</a></div>
</template>
</paper-collapse-group>
</template>
</template>
我也是Polymer的新手,所以谢谢你帮助我学习!
答案 0 :(得分:5)
使用as
属性为item
属性使用自定义名称。
示例:
<template>
<iron-ajax
auto
url="somelist.json"
handle-as="json"
last-response="{{someList}}"></iron-ajax>
<template is="dom-repeat" items="{{someList.books}}" as="book">
<paper-collapse-group>
<paper-collapse-item header$="{{book.title}}">
<p>{{book.blurb}}</p>
</paper-collapse-item>
<template is="dom-repeat" items="{{book.authors}}" as="author">
<a href$="/[[book.slug]]/[[author]]">{{author}}</a></div>
</template>
</paper-collapse-group>
</template>
</template>
答案 1 :(得分:1)
所以你可以做的是创建另一个元素,假设看起来像:
/u\b/
然后将其注入您的代码,如:
<dom-module id="authors">
<template>
<template is="dom-repeat" items="[[authors]]">
<a href$="/[[slug]]/[[item]]">[[item]]</a>
</template>
</template>
<script>
Polymer({
is: 'authors',
properties: {
authors: Array,
slug: String
},
});
</script>
</dom-module>