我有一个“卡片”元素列表;每个都有一个纸张输入元素和一个按钮元素;每个元素都从外部 JSON 中获取一些数据(在 my-post-service 元素中,我嵌入了 core-ajax)。我的目标是,当用户在文本框中写一些东西并点击相关按钮时,用户插入的特定卡片和文本的信息被发送到另一个函数。这是这个聚合物元素的代码:
<polymer-element name="template-list">
<template>
<my-post-service id="service" posts="{{posts}}" url="http://localhost:9111/v3/objects/templates">
</my-post-service>
<div id="pcId" layout horizontal wrap>
<template repeat="{{post in posts}}">
<my-post-card>
<img src="http://localhost:9111/v3/resources/{{post.representation[0].icon}}" width="70" height="70">
<h2>{{post.name}}</h2>
<p>{{post.description}}</p>
<paper-input name="paper-input" id="paperInputId" inputValue="{{value}}" label="Insert new object name..." floatingLabel="false"></paper-input>
<add-object-button on-click="{{fetchName}}" template="{{post.name}}" name="{{objName}}"></add-object-button>
</my-post-card>
</template>
</div>
<style>
:host {
display: block;
width: 100%;
}
my-post-card {
margin-bottom: 30px;
}
</style>
</template>
<script>
var objName;
Polymer({
fetchName: function () {
objName = this.$.pcId.querySelector('#paperInputId').value;
},
get objName() {
return objName;
},
});
</script>
目前,当我点击某个按钮时,我总是能够获取特定的卡片信息(通过模板属性),但我只能从第一个纸质输入元素(第一张卡片之一)中检索文本。
Ps 我是聚合物的新手,所以也许我错过了一些愚蠢的东西......!