我在angular6中有一个简单的折线图。我需要获取其图表属于图表的onclick的图表的div ID。这里的div ID是chart1,所以我需要在警报中获取它,还需要从图表外部调用。我已经尝试过了,但是它的无法正常工作,显示为空白。是否有解决方案。这是下面的代码。实时演示https://stackblitz.com/edit/angular-yw3xwa?file=src%2Fapp%2Fapp.component.ts
<hello name="{{ name }}"></hello>
<div (click)="onClickMe($event)" id="chart1"></div>
declare var require: any;
import { Component } from '@angular/core';
import * as Highcharts from 'highcharts';
import * as Exporting from 'highcharts/modules/exporting';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
clickMessage = '';
name = 'Angular';
onClickMe(event) {
alert(event.target.id);
}
ngOnInit(){
this.chartFunc('chart1');
}
chartFunc(chartId){
Highcharts.chart(chartId,{
chart: {
type: "spline"
},
title: {
text: "Monthly Average Temperature"
},
series: [
{
name: 'Tokyo',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2,26.5, 23.3, 18.3, 13.9, 9.6]
}
]
});
}
}
答案 0 :(得分:1)
没有必要传递整个事件(除非您需要的活动与您声明的其他方面不同)。实际上,不建议这样做。您只需稍作修改即可传递元素引用。
解决方案1
html
<div #referenceKeyName (click)="onClickMe(referenceKeyName)" id="chart1"></div>
组件
onClickMe(referenceKeyName) {
alert(referenceKeyName.id);
}
从技术上讲,您不需要查找被单击的按钮,因为您已经传递了实际的元素。
解决方案2
html
<div (click)="onClickMe($event)" id="chart1"></div>
组件
onClickMe(event: Event): void {
let elementId: string = (event.target as Element).id;
// do something with the id...
}