In my Angular 9 app I need to add the svg file as an interactive image.
The svg itself is in my-file.svg
and looks like this:
JavaScript
x
5
1
<svg>
2
<polygon class="my-polygon-class" points="31.7793,138.705 39.7885,138.705 39.7885,128.848 31.7793,128.848 "/>
3
<!-- lots of other polygons and paths -->
4
</svg>
5
I have a component:
JavaScript
1
13
13
1
@Component({
2
selector: 'car-view',
3
templateUrl: './car-view.component.html',
4
styleUrls: ['./car-view.component.scss']
5
})
6
export class CarViewComponent {
7
constructor() {
8
}
9
10
elementClicked() {
11
}
12
}
13
And in the car-view.component.html
I include this svg as:
JavaScript
1
4
1
<div class="car-view">
2
<object data="../assets/my-file.svg" type="image/svg+xml"></object>
3
</div>
4
How can I call the elementClicked()
function inside CarViewComponent
on click event on my-polygon-class
(which is inside the svg)? I also need to toggle another class (say element-clicked
) in order to mark the svg polygon as clicked.
Advertisement
Answer
add it in this way
JavaScript
1
7
1
<div (click)='elementClicked()'>
2
<svg>
3
<polygon class="my-polygon-class" points="31.7793,138.705 39.7885,138.705 39.7885,128.848 31.7793,128.848 "/>
4
<!-- lots of other polygons and paths -->
5
</svg>
6
</div>
7