重复时的聚合物事件参数 [英] Polymer event parameters on repeat

查看:64
本文介绍了重复时的聚合物事件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Polymer,这是一个很新的东西.

I am working on Polymer and pretty new in this.

我有一个使用模板进行迭代的列表.上面需要有一个删除按钮.在单击删除时,我正在调用一个函数.我们如何知道调用了哪个列表项,以便可以删除该项.

I have a list which iterates using a template. There needs to be a remove button on that. On click of remove I am calling a function. How do we know which list item was called so that I can have the item to remove.

<template repeat="{{ data in listData }}">
<div>
   <img src="../../Styles/images/edit.png" alt="">
   <img src="../../Styles/images/remove.png" alt="">
</div>
</template>

我之前是在这样删除

<img src="../../Styles/images/remove.png" alt="" id="data.id" on-click={{remove}}"">

因此,在remove函数上,我使用函数处理程序中的event参数获取了ID.现在,我也有此编辑.因此,现在以相同的方式,我将获得一个用于编辑的ID,如下所示.

So, on remove function I get the Id using the event parameter in the function handler. Now, I have this edit as well. So, now in the same approach, i will have an id for the edit as below.

<img src="../../Styles/images/edit.png" alt="" id="data.id" on-click={{remove}}"">

由于两个ID不能相同,因此我可以在ID后面附加一些文本,以使其与众不同.但是,我不赞成这种方法.任何人都可以对如何响应重复模板中的事件进行了解,以便我们知道需要哪个项目.

Since both ids cannot be same, I can append some text also along with the id to make it different. However I am not in favour of this approach. Can anyone throw some light on how to respond to events within a repeat template so that we can know which item was called for.

谢谢, 总结

推荐答案

传递给事件处理程序的事件的目标字段引用该项目.元素的templateInstance是绑定模型.

The target field of the event passed to the event handler refers the item. The templateInstance of the element refers the bound model.

selectStory: function(e, detail, sender) {
  var story = e.target.templateInstance.model.s;
  console.log("Clicked " + story.headline);
  this.loadStory(story.id); // accessing non-rendered data from the model
}

另请参见 https://www.polymer-project.org/docs/polymer/databinding.html (页面底部)

您也可以使用您的方法,仅使用另一个属性名称.据我所知,仍然不鼓励绑定到id.

You could also go with your approach and just use another attribute name. Binding to id is discouraged anyway as far as I know.

这篇关于重复时的聚合物事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆