【发布时间】:2020-03-31 21:22:41
【问题描述】:
在单击事件工具提示下的自定义工具提示中的以下代码 sn-p 上,当我尝试使用它时无法访问类变量,它仅显示与 ChartElement 相关的值
@Output() valuechange = new EventEmitter<any>();
options: ChartOptions = {
responsive: true,
maintainAspectRatio: false,
tooltips: {
displayColors: false,
mode: 'nearest',
intersect: false,
enabled: false,
custom(tooltipModel: any) {
// tooltip element
let tooltipEl: any = document.querySelector('#chartjs-tooltip');
// create element on first render
if (!tooltipEl) {
tooltipEl = document.createElement('div');
tooltipEl.id = 'chartjs-tooltip';
tooltipEl.style.width = '124px';
tooltipEl.style.color = 'white';
tooltipEl.style.borderRadius = '6px';
tooltipEl.style.backgroundColor = 'rgba(55, 71, 79, 0.8)';
tooltipEl.innerHTML = '<div style ="display:flex;flex-direction:column">sample tooltip</div>';
document.body.appendChild(tooltipEl);
}
tooltipEl.onclick = () => {
// NOT ABLE TO Access this to emit event
// this.valuechange.emit('test');
console.log('hi); // working
};
}
}
【问题讨论】:
-
Arrow functions 没有像“正常”函数那样定义
this。 -
甚至 tooltipEl.onclick = function () { // 不工作 }
标签: javascript angular chart.js ng2-charts