39

I have created a factory/service in my angular app.

I want to debug it in the browser. Is there a way that I can access its instance and check what is the value of its functions and variables.

The angular scope can be accessed using

angular.element(e).scope()

Is there a similar way to access factories ?

murtaza52
  • 46,887
  • 28
  • 84
  • 120

5 Answers5

67

I believe you can use something like this:

angular.element(e).injector().get('serviceName')

And since angularjs services are singletons, doing this on any angular.element will always return the same service instance/object.

bmleite
  • 26,850
  • 4
  • 71
  • 46
  • 4
    +1 This is great. Debugging becomes a lot easier with this. Adding a link to the angular documentation will improve the utility even further. ref: http://docs.angularjs.org/api/angular.element – rubish Mar 13 '13 at 17:33
  • 1
    Is this still valid? It was working fine before. But now it says `ReferenceError: e is not defined` – Ivan Wang Oct 17 '14 at 01:36
  • 2
    @IvanWang yes, it's still valid. Just tested and it's working. Are you sure the `e` variable is defined and accessible? (could it be a problem like [this](http://stackoverflow.com/questions/13326351/uncaught-referenceerror-e-is-not-defined)?) – bmleite Oct 17 '14 at 08:51
  • 1
    @bmleite That could be the reason. Would you please tell me how to define `e` in AngularJs? – Ivan Wang Oct 20 '14 at 23:40
  • 1
    @IvanWang `e` represents a local variable containing a reference to the element on which your Angular application (or is it controller?) is defined. You need to define it with `var e = document.body` or whatever is appropriate for your situation. – Steve Oct 22 '14 at 19:34
  • 2
    In Chrome, you can select an element in the elements tab and reference it with $0. So you could select your main controller element, use `angular.element($0).injector().get('serviceName')` – Geoff May 25 '16 at 18:38
8

Inject your service into any controller and then console.log(myService);

Fiddle.

Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
6

Like @bmleite said, you can use:

angular.element( {DOM element} ).injector().get('serviceName')

You can select the wanted DOM element (use 'Elements' tab on Chrome Developper Tools or 'HTML' tab on Firefox/Firebug) and then use $0 to point on it in the 'Console' tab.

The simplest element to select is the one you have your ngApp directive on it. The final code is:

var myService = angular.element($0).injector().get('serviceName');

Then you have access to all his variables and functions:

myService.getConfig('dev');
bertrandg
  • 3,147
  • 2
  • 27
  • 35
4

Try ng-inspect chrome extension.

$get('serviceName') in console will return the service or factory instance.

Vinay K
  • 5,562
  • 1
  • 18
  • 27
3

I'm not certain about how it inspects services as such, but Angular Batarang is a great Chrome extension for debugging Angular apps. Might be helpful.

https://chrome.google.com/webstore/detail/angularjs-batarang/ighdmehidhipcmcojjgiloacoafjmpfk

Useful documentation for Batarang:

http://www.c-sharpcorner.com/UploadFile/9c2805/getting-started-with-angularjs-batarang/

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
Simon East
  • 55,742
  • 17
  • 139
  • 133