1

I have a typescript class in my angular project like this:

export class CreateGeojsonLayerCommand implements Icommand {
    parameters: Object;

    execute(parameters: Object): Object {
        this.parameters = parameters;

        let layer = new Vector({            
            style:  createStyleFunction
        });
    }

    private createStyleFunction(feature: any): Style {
        //this.parameters is undefined here

    }
}

The new Vector object is a third party tool object. So I have create the createStyleFunction out of the execute method. But I can not access parameters variable using this keyword like this.parameters.

barteloma
  • 6,403
  • 14
  • 79
  • 173
  • 3
    this seems to be a scope probleme. you should use an arrow function like this "style: () => { this.createStyleFunction(); }" – Random Aug 03 '18 at 20:16
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – ConnorsFan Aug 03 '18 at 20:17

2 Answers2

2

You should use an arrow function, otherwise this refers to the particular context,

private createStyleFunction = (feature: any): Style => {

}
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

Make sure that the execute function gets called before you use createStyleFunction otherwise parameters will not have been instantiated. Or add a constructor to the class to instantiate parameters.

MkMan
  • 1,779
  • 1
  • 14
  • 27