How can I send the id along with an event in Typescript

0

I have the following defined method:

elclick(id:string) {
    console.log('El id es: ' + id);
}

And my html is this:

<a id="0" (click)="elclick(this.id);">Button</a>

What is the way this is done?

    
asked by r0b0f0sn0w 24.07.2018 в 21:02
source

1 answer

0

Because of the tags that you have put on to understand that you use angular for this case.

You could use the following:

import {Component} from 'angular2/core';

@Component({
    selector: 'my-app',
    template: '
    <button (click)="onClick($event)" id="test">Click</button>
  '
})
export class AppComponent {
  onClick(event) {
    console.log(event);
    console.log(event.srcElement.attributes.id);
    var idAttr = event.srcElement.attributes.id;
    var value = idAttr.nodeValue;
    console.log(value);
  }
}

Comparing with the implementation of javascript changes the fact that the this is not sent but the click event event is sent directly; this event has all the information you need, in this case the Source srcElement element that would be your button and its respective attributes attributes , like id event.srcElement.attributes.id

    
answered by 25.07.2018 / 17:54
source