-2

I have hyperlink with id parameter

<a [routerlink]="[]" class="someclass" id="12">test</a>

In script I add function call to this element by this

this.elementRef.nativeElement.querySelector('.someclass').addEventListener('click', this.someFunc.bind(this));

this is someFunc

   someFunc(param){
     console.log('someFunc: '+param);
   }

When I run app and press that link I get this in console:

someFunc: [object MouseEvent]

How can I send 'id' param of hyperlink to that function ?

Trying solution from Aakash Garg

this.elementRef.nativeElement.querySelector('.someclass').addEventListener('click', (e) => this.someFunc(e.target.id));

For testing printing out straght:

(e) => console.log('test: '+e.target.class) > prints out test: undefined
 (e) => console.log('test: '+e.target.id) > prints out test: 
David
  • 4,332
  • 13
  • 54
  • 93

2 Answers2

1
this.elementRef.nativeElement.querySelector('.someclass').addEventListener('click', (e) => this.someFunc(e.id));
Aakash Garg
  • 10,649
  • 2
  • 7
  • 25
-1

You can use an angular click-handler. Adding event listeners the way you did has the problem that it may run outside of angular zone and cause problems.

<a [routerlink]="[]" class="someclass" (click)="someFunc('12')" id="12">test</a>
Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59