Here is another take for Angular 9.
Open the JavaScript console on the page of your Angular application that is not in the production node.
Find the <app-root>
on the page:
root = document.querySelector("app-root");
Get the injector. The ng()
helper is a global that is available if the app is in development mode:
injector = ng.getInjector("app-root");
Injector would allow you to get services by their class name. Unfortunately, I do not know if or how Angular service classes are available through the window
namespace.
So our next attempt is to get services from the components of the already visible elements on the page.
appComponent = ng.getComponent(root)
This way you get access to any AppComponent
variables e.g. "UserService" your app might have.
appComponent.userService
UserService {http: HttpService, userSubject: BehaviorSubject}
Now you can call the service from the console:
appComponent.userService.userSubject.value.userEmail
mikko@capitalgram.com
You can also access components that are not the application root, and get access to services bound to them. First we need to navigate to the page where the component appears. This will trigger the actual transition in the UI.
await appComponent.router.navigate(["/another-page"])
Now you can get hold of any visible component by its tag name:
elem = document.querySelector("app-my-component")
component = ng.getComponent(elem)
And you can see its services:
MyComponent(envService, http, sanitizer);
And you can poke them, with all your user tokens, sessions, etc. in place:
component.envService.getConfigValue()