0

Let's say I have following img tag:

<img *ngFor="let icon of icons" [src]="item.imageSrc">

Where icons is array of Icon objects:

export class Icon {
    name;
    imageSrc;
    description;
}

I need to handle event on this img tag element and I would like to see in the event also the object icon. I have some ideas - for example just creating Component, or filtering list by name which is unique with changing a little bit html code to:

<img *ngFor="let item of icons" [src]="item.imageSrc" [name]="item.name" >

However I feel it's not proper way for this issue. Can i give a property to img tag that will store this object?

Michał Lis
  • 461
  • 1
  • 6
  • 19
  • I think that's what the `data-` attributes were created for, however I don't really know how to feel about storing data in the view layer. You can read about `data` [here](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) – Osman Cea Dec 11 '17 at 13:27
  • I have found the topic which recommends using attr instead of data-* for Angular projects. [Topic here](https://stackoverflow.com/questions/34542619/angular-2-data-attributes) However this topic does not solve my problem. It works only for Strings, not for whole objects - however I try to modify it. – Michał Lis Dec 11 '17 at 15:30
  • I'd just use the filter approach you mentioned to be honest. – Osman Cea Dec 11 '17 at 15:33

0 Answers0