5

How do I inject a service in angular2 in the console?

In angular1 just wrote:

angular.element(document).injector().get('MyService')
Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
JV3
  • 872
  • 2
  • 9
  • 21
  • 2
    May this blogpost helps http://juristr.com/blog/2016/02/debugging-angular2-console/ – achingfingers Jul 06 '16 at 13:08
  • just tried. ng.probe($0) returns undefined – JV3 Jul 06 '16 at 13:14
  • ahhhh it works. But only, if you select the root component in the browser developer tools before. That's no option. Is there any option to select the root component automatically without $0 ? – JV3 Jul 06 '16 at 14:55
  • 2
    ng.probe($0).componentInstance.MyService.myMethodCall() works, but i want it without $0. Any idea? – JV3 Jul 06 '16 at 14:57
  • Possible duplicate of [Accessing root Angular 2 injector instance globally](https://stackoverflow.com/questions/36455305/accessing-root-angular-2-injector-instance-globally) – Zachary Moshansky Feb 14 '18 at 21:28

0 Answers0