How to pass parameters in Polymer 2.0 on-tap function?

≯℡__Kan透↙ 提交于 2019-12-04 08:03:20

Another solution could be the dataset object within the event.target. You can define your properties with the data- prefix:

<div on-tap="doSomething" data-item$="[[item]]"></div>

And within your doSomething() listener you can get the dataset object:

doSomething(event) {
  const item = event.target.dataset.item;
  ...
}

You can access the model via the event argument's model property.

So you can access the index from event.model.index.

Kuba Šimonovský

Well, I am aware you can't. There are many discussions about that on the internet.

Of course, there is a way how to pass argument to function. You can save [[index]] in attribute of element and then get the attribute when needed.

Example:

<div on-tap='_deleteNote' indexed$='[[index]]'>

Then in script:

_deleteNote(e) {
  var index = e.target.getAttribute('indexed');
  ...
}

Once you get index, you can do whatever you want with it.

Don't forget to extend Polymer gestures if you are not using hybrid elements in Polymer 2.0.

class Foo extends Polymer.GestureEventListeners(Polymer.Element) {}

More about it: https://www.polymer-project.org/2.0/docs/devguide/gesture-events#using-gesture-events

What is hybrid element: https://www.polymer-project.org/2.0/docs/devguide/hybrid-elements

on-tap does not appear to be implemented in Polymer 2.0. If you instead use on-click then it will work.

EDIT: see below comment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!