angulartypescriptionic-frameworkngoninitblockly

Access local var from ngOnInit from another function


I am new to Angular and TS. I am trying to access a local var that is in ngOnInit from outside it, but I don't know how to do it properly. I have a component called BlocklyComponent where I created the var.

    export class BlocklyComponent implements OnInit {
      primaryWorkspace: Blockly.WorkspaceSvg;
      constructor(private sanitizer: DomSanitizer) {
    this.updateXML = this.updateXML.bind(this);
  }
  ngOnInit() {
    var primaryWorkspace = Blockly.inject('primaryDiv',{....} as Blockly.BlocklyOptions);

Inside the NgOnInit function it works correctly, I can see the object without any problem. However when I add the addchangeListener to read when there is an event I cant access that local var from the new function that is outside ngOnInit.

primaryWorkspace.addChangeListener(this.updateXML); //addChangeListener is special for Blockly library
  }
  updateXML(primaryEvent ) 
  {
    if (primaryEvent.isUiEvent) 
     {
       return; //Do not clone ui events 
     };
    var xml = Blockly.Xml.workspaceToDom(primaryWorkspace); //XML machine. Blockly is a library.
    console.log(xml);
  } 

I tried to add this (this.primaryWorkspace) to the call but it takes the property not the local var. I know that is possible to create the function inside ngOnInit and it works but I need to create updateXML outside. Thank you very much


Solution

  • Finally, it was a simple problem. I solved the issue myself by declaring the var as this.primaryWorkspace instead of declaring it as a VAR.