How to change the behavior of `this' in the below code. Currently, it is pointing to a click event on the HighChart. I want `this` here to refer to function in the class.
Hello.ts
getPlotBands() {
.....
click: function(e) {
for(var p = 0; p < this.axis.plotLinesAndBands.length; p++) {
var currentPlotBand = this.axis.plotLinesAndBands[p];
currentPlotBand.svgElem.attr({fill: currentPlotBand.options.color});
}
this.svgElem.attr({fill:'#660066'});
// this.fromSystemTime = String(new Date(this.options.from));
this.fromSystemTime = moment(this.options.from).format("MM/DD/YY h:mm:ss");
this.toSystemTime = String(new Date(this.options.to));
console.log(moment(this.options.from).format("MM/DD/YY h:mm:ss"));
(<HTMLInputElement>document.getElementById('fromSys')).value =
moment(this.options.from).zone("America/Los_Angeles").format('MM/DD/YY HH:mm');
(<HTMLInputElement>document.getElementById('toSys')).value =
moment(this.options.to).zone("America/Los_Angeles").format('MM/DD/YY HH:mm');
console.log('to date -', moment(1556906400000).utcOffset(-420).format('MM/DD/YY HH:mm'));
console.log(moment(1556906400000).zone("America/Los_Angeles").format('MM/DD/YY HH:mm'));
// console.log('from', this.options.from);
// console.log('to', this.options.to);
console.log(this.options.from + 'and' + this.options.to)
tagStartTimeForDetailsPage = +moment(this.options.from);
tagEndTimeForDetailsPage = +moment(this.options.to);
` this.storedResponse.setTagStartTimeForDetailsPage(+tagStartTimeForDetailsPage);
this.storedResponse.setTagEndTimeForDetailsPage(+tagEndTimeForDetailsPage);
}` }
I want here `this' to refer my storedResponse service injected in the class not chart obj.
The below 2 lines are giving the error
`this.storedResponse.setTagStartTimeForDetailsPage(+tagStartTimeForDetailsPage);
this.storedResponse.setTagEndTimeForDetailsPage(+tagEndTimeForDetailsPage); `
上面的代码位于Hello.Ts文件中,该文件具有1个称为getplotBands()的方法。因此,在这里我需要更改此上下文。
知道如何完成吗?
答案 0 :(得分:1)
使用箭头功能代替function
关键字:
click: (e) => {
// ...
}
关于此的很多问题,虽然找不到:)