javascript - 在 Angular 中捕获 SVG 内的类的点击事件
问题描述
在我的 Angular 9 应用程序中,我需要将 svg 文件添加为交互式图像。
svg 本身在my-file.svg
其中,如下所示:
<svg>
<polygon class="my-polygon-class" points="31.7793,138.705 39.7885,138.705 39.7885,128.848 31.7793,128.848 "/>
<!-- lots of other polygons and paths -->
</svg>
我有一个组件:
@Component({
selector: 'car-view',
templateUrl: './car-view.component.html',
styleUrls: ['./car-view.component.scss']
})
export class CarViewComponent {
constructor() {
}
elementClicked() {
}
}
在car-view.component.html
我将这个 svg 包括为:
<div class="car-view">
<object data="../assets/my-file.svg" type="image/svg+xml"></object>
</div>
如何在点击事件(在 svg 内部)上调用elementClicked()
内部函数?我还需要切换另一个类(例如)以将 svg 多边形标记为已单击。CarViewComponent
my-polygon-class
element-clicked
解决方案
以这种方式添加
<div (click)='elementClicked()'>
<svg>
<polygon class="my-polygon-class" points="31.7793,138.705 39.7885,138.705 39.7885,128.848 31.7793,128.848 "/>
<!-- lots of other polygons and paths -->
</svg>
</div>
推荐阅读
- python - 在 arcgisimage 上绘制位置点
- kubernetes - 需要设置自定义 Kubernetes 日志记录策略
- javascript - 如何使用 aws lambda 和 nodejs 创建和发送 csv?
- google-chrome-extension - chrome 扩展 - 我如何等待 chrome.runtime 功能?
- notepad++ - 用文本替换 Notepad++ 中的空行(仅限 LF)
- arduino - 警告:在 { } [-Wnarrowing] 内将 'tempo' 从 'int' 缩小到 'unsigned char' 的转换是我的代码
- node.js - 在mongodb中按月份和日期分组并显示列表
- angular - Angular 12,为什么路由器防护不起作用?
- javascript - 在 vuetify 小吃店中重置超时
- apache-spark - 在 Spark 3.1.1 java 中添加新列作为数组