0

I would like to call my ApiService from a browser console.

I tried what explained in Call a service function from browser console in angular 6:

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(private http: HttpClient) {window['myService'] = this}

Which is causing compiling error:

Error: ../services/api.service.ts:12:49 - error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.

12   constructor(private http: HttpClient) {window['ApiService'] = this; }

I also tried to run this from console:

angular.element(document).injector().get('ApiService');

But angular dependencies do not seem to be loaded as I get the following error:

Uncaught ReferenceError: angular is not defined
    <anonymous> debugger eval code:1

Any clue to progress on this topic?

blondelg
  • 916
  • 1
  • 8
  • 25
  • Does this answer your question? [Call a service function from browser console in angular 6](https://stackoverflow.com/questions/54212288/call-a-service-function-from-browser-console-in-angular-6) – yehonatan yehezkel May 30 '21 at 11:28
  • 1
    @yehonatanyehezkel, I have already tried what explained in this post, and I get the error described in my question. I wish I could have it work – blondelg May 30 '21 at 11:29
  • Its working fine https://stackblitz.com/edit/angular-ivy-aajstl?file=src%2Fapp%2Fapp.component.ts @blondelg – Developer May 30 '21 at 13:40
  • Just in case, did you copy paste the answer ? Try to do this in the console `console.log(window['myService'])`. Because in your question you defined the global variable in `window['myService']` and not `window['ApiService']` – Martin Paucot Jun 01 '21 at 07:20

0 Answers0