如何从点击事件中获取纸卡的数据属性值
How to get data-attribute value of paper-card from on-tap event
目标是在从 ontap
事件调用的方法 openForm
中获取 paper-card
的 data-id
属性的值。
两次尝试都失败了。这是我的代码:
<div class="layout horizontal wrap">
<template is="dom-repeat" items="{{itemList}}">
<paper-card on-tap="openForm" data-id="{{item.id}}">
<paper-ripple></paper-ripple>
<paper-item>
<iron-icon class="big" src$="{{setIcon(item)}}"></iron-icon>
<paper-item-body two-line>
<div>{{item.fullNameRev}}</div>
<div secondary style$="{{color(item.groupColor)}}">{{item.group}}</div>
</paper-item-body>
</paper-item>
</paper-card>
</template>
</div>
Polymer({
is : "my-element",
openForm: function(e) {
e.srcElement.getAttribute("data-id"); // does not work
e.target.getAttribute("data-id"); // neither works
},
color: function(c) {
return "color:" + c;
}
});
我想您正在寻找的答案可以在这里找到:
Handling events in dom-repeat templates
比我解释得更好,但我想简而言之,它创建了一个 'model' 可访问的 e.model.item.data-id 用于发送事件的纸卡。我认为您的代码看起来类似于:
Polymer({
is : "my-element",
openForm: function(e) {
var model = e.model
model.item.id, //think this will be the value
color: function(c) {
return "color:" + c;
}
});
目标是在从 ontap
事件调用的方法 openForm
中获取 paper-card
的 data-id
属性的值。
两次尝试都失败了。这是我的代码:
<div class="layout horizontal wrap">
<template is="dom-repeat" items="{{itemList}}">
<paper-card on-tap="openForm" data-id="{{item.id}}">
<paper-ripple></paper-ripple>
<paper-item>
<iron-icon class="big" src$="{{setIcon(item)}}"></iron-icon>
<paper-item-body two-line>
<div>{{item.fullNameRev}}</div>
<div secondary style$="{{color(item.groupColor)}}">{{item.group}}</div>
</paper-item-body>
</paper-item>
</paper-card>
</template>
</div>
Polymer({
is : "my-element",
openForm: function(e) {
e.srcElement.getAttribute("data-id"); // does not work
e.target.getAttribute("data-id"); // neither works
},
color: function(c) {
return "color:" + c;
}
});
我想您正在寻找的答案可以在这里找到: Handling events in dom-repeat templates
比我解释得更好,但我想简而言之,它创建了一个 'model' 可访问的 e.model.item.data-id 用于发送事件的纸卡。我认为您的代码看起来类似于:
Polymer({
is : "my-element",
openForm: function(e) {
var model = e.model
model.item.id, //think this will be the value
color: function(c) {
return "color:" + c;
}
});