Let's analyze the code snippet below:
HTML:
<button mat-flat-button id="test" (click)="toggle($event)">Click me!</button>
Component:
toggle(event) {
let id: string = (event.target as Element).id;
console.log(id)
console.log(event.target)
}
When the button is clicked, it triggers the toggle
function with the event as a parameter.
The system will log "test"
(id) and a button event.
However, pressing the text on the button will result in logging an undefined id and a span
with the class mat-button-wrapper
.
Question: How can I obtain the button event target when the text on the button is pressed instead of the span event target?