1

I'm running Angular 8. Following the probably outdated advice here -- How can I test an AngularJS service from the console?, I'm trying to access an Angular service from the Chrome devtools console. Below is the disappointing output

Angular is running in the development mode. Call enableProdMode() to enable the production mode.
injector = angular.element(document.body).injector()
VM914:1 Uncaught ReferenceError: angular is not defined
    at <anonymous>:1:1

What's the right way to access Angular on the command line in Angular 8?

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

1

you can just save a reference of injector class globally

main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';


if (environment.production) { //  production
  enableProdMode();
} else { //  development
  const platform = platformBrowserDynamic();
  window['injector'] = platform.injector;
}

Injector

Updated!!

onther option without store a reference of the injector globaly

const inj = ng.probe(document.querySelector('app-root')).injector;
Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
  • Thanks but I'm trying to do this without modifying my existing code. It seemed possible in older version of Angular – Dave Sep 11 '19 at 22:54
  • click on `app-root` then `const e =$0; ng.probe(e).injector` – Muhammed Albarmavi Sep 11 '19 at 23:01
  • check this answer https://stackoverflow.com/questions/36455305/accessing-root-angular-2-injector-instance-globally hope will help you – Muhammed Albarmavi Sep 11 '19 at 23:07
  • Hi @malbarmawi, When I type "const e =$0" into Chrome's devtoosl console, I get an "undefined" message. The answer you referenced, that's TypeScript ... I'm trying to figure out how to access it from teh browser console, unless I'm misreading what's in the accepted answer. – Dave Sep 11 '19 at 23:29
  • $0 is just a ref for any item you have click on it in the element tab so just look for app-root and click on it then the $0 will point to that element – Muhammed Albarmavi Sep 11 '19 at 23:31
  • or this way `ng.probe(document.querySelector('app-root')).injector` – Muhammed Albarmavi Sep 11 '19 at 23:33
  • Thx. If you are so inclined, I have a related question here -- https://stackoverflow.com/questions/57898015/in-angular-8-how-do-i-access-an-injected-service-from-the-browser-console . – Dave Sep 11 '19 at 23:41