4

I need to call the class method through console which intern accesses the class variable and then it reflects on the HTML. I Referred Call a service function from browser console in angular 6 but I was unable to access this keyword.

export class AppComponent {
    title = 'calender-app';
    timeslot: any =  [];

    constructor(){
      window['plotEvent'] = this.plotEvent;
    }
  
    plotEvent(events: Calender[]): void{
    // not able to access this.timeslot when I call it from browser's console.
    this.timeslot.push(...events); // getting error as can't read property timeslot of undefined.
    }
Akash Mane
  • 129
  • 1
  • 8

1 Answers1

4

I assume that you are trying to access a typescript method on the browsers console.

You can make the entire angular component.ts's variables, methods etc. available to access in the browser console by adding OnInit or in the constructor the following:

Let's say you are trying to access a method within the AppComponent, you go.

OnInit() {
    window['AppComponent'] = this;
}

Or:

constructor() {
    window['AppComponent'] = this;
}

The same works in services too.

Then in the console you go: AppComponent.myMethod()

Ahron M. Galitzky
  • 2,219
  • 2
  • 13
  • 25