I'm trying to capture mouse coordinates in a Angular component.
Here is the html in question:
<div id="container" class="fullSize" style="height:100%;" (click)="onClick(ev)"></div>
Here is the component function in the corresponding typescript file:
onClick( ev : MouseEvent ) {
console.log("x:" + ev.clientX);
}
And the error in the console:
Cannot read property 'clientX' of undefined'
What would be the correct way of passing that MouseEvent to my component function?
You should use $event
to pass MouseEvent
object to your click handler.
So simply write:
(click)="onClick($event)"
See also
Current best practice:
<div id="container" class="fullSize" style="height:100%;" (click)=onClick($event.clientX)"></div>
onClick(myClientX: number) {
console.log("x: " + myClientX);
}
Per current guideline found here: Passing $event is a dubious practice
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