Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing class method from console in angular typescript

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.
    }
like image 547
Akash Mane Avatar asked Oct 24 '25 02:10

Akash Mane


1 Answers

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()

like image 95
Ahron M. Galitzky Avatar answered Oct 26 '25 16:10

Ahron M. Galitzky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!