The OP's demo showed a correct example of listening for click events on an SVG element with Angular.
To simplify a bit, the main.html
file can be as follows:
<svg width="500" height="500" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<path
(click)="click($event)"
style="fill: none; stroke: #000000; stroke-width: 1px;"
d="M 10.280374,48.130842 V 26.168224 H 52.336448 V 70.794392 H 26.869158 V 50 h -7.476635"
/>
</svg>
I'll also copy OP's code for main.ts
from StackBlitz so that other people can test it in the future:
import 'zone.js/dist/zone';
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
@Component({
selector: 'my-app',
standalone: true,
imports: [CommonModule],
templateUrl: `./main.html`,
})
export class App {
name = 'Angular';
click(e: MouseEvent) {
console.log('DING DONG', e.target);
}
}
bootstrapApplication(App);
The above will work with e.target
pointing to the correct path element.
The actual cause of "element's fill area not clickable" is something else. Since there is no fill (due to the CSS property fill:none
), by default you can only click on the stroke. And when the stroke is thin, it's hard to click the element.
If you don't have to support old browsers, the solution is by adding a css property pointer-events: all
to the path element.
(For more info about this property, see https://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events )
To support older browsers, you can use fill: rgba(0,0,0,0)
or fill: transparent
, or any fill color with fill-opacity: 0
. This is less performant, though, as a browser may still try to paint a 'transparent color' over the background in those regions (although the result is exactly the same not painting over the background, it's like multiplying by 1 and adding by 0)