marcXandre
marcXandre

Reputation: 2432

Callabable function in browser console with Angular 2+

We have an application made with Angular 9 that call an API.

The goal is to change the API URL to call the one deployed locally without having the Angular application running locally:

>  changeUrl("localhost:8080")

APIs URL as changed from "example.com" to "localhost:8080" 

After this call, the application should make requests to localhost:8080.

Is there a way to change an Angular application variable from the browser debug console in runtime? Preferable using a custom function.

Also, where should I define this function/code to be able to call it at runtime in production mode?

Upvotes: 0

Views: 36

Answers (1)

alt255
alt255

Reputation: 3566

You can add attach changeUrl to your window object in your app.component.ts e.g

...
class AppComponent {

  constructor() {
    (window as any).changeUrl = this.changeUrl.bind(this);
  }

  changeUrl(url) {
    //.....changeUrl implementation
  }
}

It can be latter called from dev console using: window.changeUrl(url)

Upvotes: 2

Related Questions