0

I want to access in class property and method

//sample code

export class MapComponent implements {

chartMap;

loadMap(){

   let setting={
      callBack:function(data){
         this.chartMap=data;   //<<<------not access my class property
      }
   }

}
}

not access my class property in json object

m.a
  • 3
  • 1
  • Also, there's no JSON anywhere in this question. JSON is a text format. –  Mar 10 '19 at 13:12

1 Answers1

0

There's a this context issue. when setting.callBack() is invoked, it is looking for chartMap in caller, not in the MapComponent object.

There are several way to do it. You can use Arrow function expression.

export class MapComponent implements {

  chartMap;

  loadMap() {

    let setting = {
      callBack: (data) => {
        this.chartMap = data;
      }
    }
  }
}
zmag
  • 7,825
  • 12
  • 32
  • 42