Same controllers with different data and elements

Hi,

I call several times the same controller but i don’t have different data or element because it’s the same controller.

<div data-controller='slideshows'>
  <div data-controller='slideshow' data-slideshow-index="1">...</div>
  <div data-controller='slideshow' data-slideshow-index="4">...</div>
</div>

How to get the good element and the good data by controller ?

You can do something like this

<div data-controller='slideshows'>
  <div data-controller='slideshow' data-slideshow-index="1" data-action="click->slideshow#handleClick">...</div>
  <div data-controller='slideshow' data-slideshow-index="4" data-action="click->slideshow#handleClick">...</div>
</div>

and then in your controller

handleClick(event) {
  console.log('index', this.data.get("index"));
}
1 Like