0

I am trying to use a function in one of my classes in a third party library(highcharts) I included in my project.

Something along the lines of:

export class TrendPage {
  ...
  functionToUse(){
    this.top = someCalculation();
  }
  ...
  this.chart = Highcharts.chart(this.chartContainer, {
    ...
        xAxis: {
            events: {
                afterSetExtremes: ()=> {
                   this.functionToUse()
                }
            }
        },
   ...

Is there a correct way to do this?

Solution I found:

Using ngZone to run the function outside the scope of the zone as in this answer Angular2 - how to call component function from outside the app

SDhaliwal
  • 174
  • 6
  • I don’t know what you want can you provide concrete example of what you trying to do and expected output? Also if the code isn’t working provide stack trace – Mike Tung Oct 18 '17 at 01:54
  • I am trying to change the member value "top" in the class TrendPage. Since I am trying to use "this.functionToUse()", the context "this" is in is the context of the chart, not the component. – SDhaliwal Oct 18 '17 at 02:01
  • 1
    I have found an answer here: https://stackoverflow.com/a/35297044/5774939 Wondering if there is a better/alternative way to achieve this – SDhaliwal Oct 18 '17 at 02:16
  • Also use ` afterSetExtremes: (event) => {` instead of `afterSetExtremes: function (event) {` otherwise `this` won't eork as expected inside the function. (search for arrow functions for more details) – Günter Zöchbauer Oct 18 '17 at 05:06

1 Answers1

0

In

this.functionToUse()

"this" is inside the scope of the function and cannot "see" for function outside. Can you try this code instead?

afterSetExtremes: ()=>
{
    this.functionToUse()
}

Be carefull because "event" variable cannot be used anymore using this anonymous function. (search more for lambda/anonymous function)

Marco Castano
  • 1,114
  • 1
  • 10
  • 25
  • Ahh yes the code I wrote above actually follows this convention (I was testing other things) will edit my original post. – SDhaliwal Oct 23 '17 at 02:22