Javascript 如何访问chart.js自定义工具提示界面中的类变量

Javascript 如何访问chart.js自定义工具提示界面中的类变量,javascript,angular,chart.js,ng2-charts,Javascript,Angular,Chart.js,Ng2 Charts,在“单击时工具提示”事件下的“自定义工具提示”中的以下代码段中,当我尝试使用该类时,无法访问该类变量,因为它仅显示与ChartElement相关的值 @Output() valuechange = new EventEmitter<any>(); options: ChartOptions = { responsive: true, maintainAspectRatio: false, tooltips: { displayColors: false, mode: 'nea

在“单击时工具提示”事件下的“自定义工具提示”中的以下代码段中,当我尝试使用该类时,无法访问该类变量,因为它仅显示与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
      };
    }
}
@Output()valuechange=neweventemitter();
选项:图表选项={
回答:是的,
MaintaintAspectRatio:false,
工具提示:{
显示颜色:假,
模式:“最近的”,
交集:错,
启用:false,
自定义(ToolTimpodel:any){
//工具提示元素
let tooltipEl:any=document.querySelector(“#chartjs tooltip”);
//在第一次渲染时创建元素
如果(!工具管){
tooltipEl=document.createElement('div');
tooltipEl.id='chartjs tooltip';
tooltipEl.style.width='124px';
tooltipEl.style.color='白色';
tooltipEl.style.borderRadius='6px';
tooltipEl.style.backgroundColor='rgba(55,71,79,0.8)';
tooltipEl.innerHTML='示例工具提示';
document.body.appendChild(工具管道);
}
tooltipEl.onclick=()=>{
//无法访问此以发出事件
//这个.valuechange.emit('test');
console.log('hi);//正在工作
};
}
}

要使
与类相等,请将其写成箭头函数:

custom: () => {
 console.log(this); // should be the class
}
export const thisAsThat = (callBack: Function) => {
    const self = this;
    return function () {
      return callBack.apply(self, [this].concat(Array.prototype.slice.call(arguments)));
    };
  }
但有时您需要一个
this
that
句柄,其中
this
是类,
that
是图表对象

创建实用程序函数:

custom: () => {
 console.log(this); // should be the class
}
export const thisAsThat = (callBack: Function) => {
    const self = this;
    return function () {
      return callBack.apply(self, [this].concat(Array.prototype.slice.call(arguments)));
    };
  }
然后:


没有以“正常”函数的方式定义。即使是ToolTipe.onclick=function(){//not working}