Let's say we have a div with 3 links inside.
<div class="wrap">
<a class="link">link1</a>
<a class="link">link2</a>
<a class="link">link3</a>
</div>
In order to assign a click handler for the links in jQuery we can do smth like this:
$('.wrap a').on('click', callback);
.
What is the best approach for that in Angular2?
From what I learned I could use a simple (click) for each link, like:
<a class="link" (click)="callback()">
, but to me it looks odd.
Another option would be to use a directive and do smth like
<a class="link" [directive]="value">
, this one I like more.
Is there an even better implementation of described case?
The click event will bubble up to the parent div so you could put a the event handler there.
<div class="wrap" (click)="clicked($event)">
<a id="link1" class="link">link1</a>
<a id="link2" class="link">link2</a>
<a id="link3" class="link">link3</a>
</div>
export class AppComponent {
clicked(event: MouseEvent) {
console.log(event.srcElement.id);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With