1

How can i apply onclick for every item within div element

<div>
<p (click)="Click(1)" value="1">1 element</p>
<p (click)="Click(2)" value="2">2 element</p>
<p (click)="Click(3)" value="3">3 element</p>
</div>

As you see, i showed the very bad way, is there any clearier and simpler method to do that. BTW. list inside div is dynamic

Dakito
  • 347
  • 1
  • 4
  • 9
  • There is a section in my answer for another question that can help you. [here](https://stackoverflow.com/a/47917853/2473163) – Sinan Sep 01 '18 at 17:48

2 Answers2

1

@Dakito try this... make an Array as values and then

    <div>
      <p (click)="Click(value)" *ngFor="let value of values" value="value">{{value}} element</p>
    </div>
Ganesh
  • 5,808
  • 2
  • 21
  • 41
0

TypeScript Class

items = [{ number: 1 }, { number: 2 }, { number: 3 }];

Template:

<div>
    <p *ngFor="let item of items" 
        (click)="Click(item.number)" 
        [value]="item.number">
        {{item.number}} element
    </p>
</div>
SiddAjmera
  • 38,129
  • 5
  • 72
  • 110