Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I would like to access my $scope variable in Chrome's JavaScript console. How do I do that?

I can neither see $scope nor the name of my module myapp in the console as variables.

share|improve this question
44  
For debugging I usually set window.MY_SCOPE = $scope; first thing in my controller function. – Jason Goemaat May 31 '13 at 1:23
2  
If you're considering development/testing in Firefox, you can also use AngScope, a small extension that displays $scope objects of selected DOM elements into Firebug's DOM Inspector. – Kos Prov Jan 15 '14 at 14:13
    
@JasonGoemaat why not use window.$scope = $scope; so that you can simply use $scope rather than MY_SCOPE - I haven't noticed any problems but maybe i'm missing a security concern or something. – James Gentes Jun 5 '15 at 15:53
4  
Just for clarity, someone new to angular might get confused and think that $scope was magically available in the console if the just saw it being used that way. Also if you then mistakenly use scope in a directive declaration and $scope in the code for instance, you'd be using that on the window object instead of getting an error. – Jason Goemaat Jun 5 '15 at 18:35

11 Answers 11

up vote 919 down vote accepted

Pick an element in the HTML panel of the developer tools and type this in the console:

angular.element($0).scope()

In WebKit, $0 is a reference to the selected DOM node in the elements tab, so by doing this you get the selected DOM node scope printed out in the console.

You can also target the scope by element ID, like so:

angular.element(document.getElementById('yourElementId')).scope()

Addons/Extensions

There are some very useful Chrome extensions that you might want to check out:

  • Batarang. This has been around for a while.

  • ng-inspector. This is the newest one, and as the name suggests, it allows you to inspect your application's scopes.

Playing with jsFiddle

When working with jsfiddle you can open the fiddle in show mode by adding /show at the end of the URL. When running like this you have access to the angular global. You can try it here:

http://jsfiddle.net/jaimem/Yatbt/show

jQuery Lite

If you load jQuery before AngularJS, angular.element can be passed a jQuery selector. So you could inspect the scope of a controller with

angular.element('[ng-controller=ctrl]').scope()

Of a button

 angular.element('button:eq(1)').scope()

... and so on.

You might actually want to use a global function to make it easier:

window.SC = function(selector){
    return angular.element(selector).scope();
};

Now you could do this

SC('button:eq(10)')
SC('button:eq(10)').row   // -> value of scope.row

Check here: http://jsfiddle.net/jaimem/DvRaR/1/show/

share|improve this answer
    
Thanks. When I try to install Batarang it tells me your computer is not supported , I have ubuntu, any ideas ? – murtaza52 Dec 6 '12 at 13:07
    
@jm- as of angular.element($0).scope(), it works until you try to call some methods. I tried, and for some reason no HTTP requests are possible in this setup? – krtek Apr 9 '13 at 13:12
    
With chrome in jsfiddle you can get to the show frame with the drop-down at the bottom of the window. Normally it says <top frame> and you want to pick result( fiddle.jsshell.net/ ) to have the console in the context of the running frame. – Jason Goemaat May 31 '13 at 1:20
6  
Note that if you're disabling the debug info you'll always get undefined using this method. This is intended and can be prevented by.. .well, not disabling the debug info on the $compileProvider – Robba Mar 18 '15 at 16:18
3  
alternative to angular.element($0).scope(): you could also do $($0).scope() – wolfman6377 Nov 18 '15 at 19:19

To improve on jm's answer...

// Access whole scope
angular.element(myDomElement).scope();

// Access and change variable in scope
angular.element(myDomElement).scope().myVar = 5;
angular.element(myDomElement).scope().myArray.push(newItem);

// Update page to reflect changed variables
angular.element(myDomElement).scope().$apply();

Or if you're using jQuery, this does the same thing...

$('#elementId').scope();
$('#elementId').scope().$apply();

Another easy way to access a DOM element from the console (as jm mentioned) is to click on it in the 'elements' tab, and it automatically gets stored as $0.

angular.element($0).scope();
share|improve this answer
    
angular contains a subset of jquery, so you can always use the later syntax (if it is correct), i'm not sure it is – Pizzaiola Gorgonzola Apr 9 '14 at 10:23
3  
Thanks for mentioning the .scope().apply() – raphinesse Apr 6 '15 at 20:49

If you have installed Batarang

Then you can just write:

$scope

when you have the element selected in the elements view in chrome. Ref - https://github.com/angular/angularjs-batarang#console

share|improve this answer
4  
The most recent version of batarang on Chrome 42 is not working anymore. – Patrick Bassut May 2 '15 at 5:57
1  
Working for me now, Chrome 44. $scope just works – sricks Aug 13 '15 at 21:55
1  
And now it's not working! Chrome 45. – Airzooka Sep 16 '15 at 21:04

This is a way of getting at scope without Batarang, you can do:

var scope = angular.element('#selectorId').scope();

Or if you want to find your scope by controller name, do this:

var scope = angular.element('[ng-controller=myController]').scope();

After you make changes to your model, you'll need to apply the changes to the DOM by calling:

scope.$apply();
share|improve this answer
2  
How does this answer have so many upvotes? You do not need jQuery for this! angular.element is already an element selection method. Stop saying you need jQuery for simple tasks like selecting an element by its id! – Tyrsius Jun 16 '15 at 23:22
2  
I didn't say you need it. What i am saying is if you already have it there you could use it like this. – BraveNewMath Jun 17 '15 at 23:58
2  
angular.element already does the thing you are using jQuery for. In fact, if jQuery is available angular.element is a alias for jQuery. You are needlessly complicating your code. angular.element('#selectorId') and angular.element('[ng-controller=myController]') do the same thing, only with less code. You might as well call angular.element('#selectorId'.toString()) – Tyrsius Jun 18 '15 at 2:29
    
@Tyrsius, perhaps your feedback could be less accusatory and angry and a bit more professional? – Tass Oct 29 '15 at 14:46
1  
@Tass You are right, I was needlessly rude. I apologize. It is enough to say that the same thing is being done twice. – Tyrsius Oct 29 '15 at 18:44

Somewhere in your controller (often the last line is a good place), put

console.log($scope);

If you want to see an inner/implicit scope, say inside an ng-repeat, something like this will work.

<li ng-repeat="item in items">
   ...
   <a ng-click="showScope($event)">show scope</a>
</li>

Then in your controller

function MyCtrl($scope) {
    ...
    $scope.showScope = function(e) {
        console.log(angular.element(e.srcElement).scope());
    }
}

Note that above we define the showScope() function in the parent scope, but that's okay... the child/inner/implicit scope can access that function, which then prints out the scope based on the event, and hence the scope associated with the element that fired the event.

@jm-'s suggestion also works, but I don't think it works inside a jsFiddle. I get this error on jsFiddle inside Chrome:

> angular.element($0).scope()
ReferenceError: angular is not defined

share|improve this answer
    
this is good. I edited my answer to show how to do it in jsfiddle using angular.element – jaime Dec 6 '12 at 21:51
    
@jm-, thanks, I didn't know about the /show capability. – Mark Rajcok Dec 6 '12 at 22:04

One caveat to many of these answers: if you alias your controller your scope objects will be in an object within the returned object from scope().

For example, if your controller directive is created like so: <div ng-controller="FormController as frm"> then to access a startDate property of your controller, you would call angular.element($0).scope().frm.startDate

share|improve this answer

I agree the best is Batarang with it's $scope after selecting an object (it's the same as angular.element($0).scope() or even shorter with jQuery: $($0).scope() (my favorite))

Also, if like me you have you main scope on the body element, a $('body').scope() works fine.

share|improve this answer

I usually use jQuery data() function for that:

$($0).data().$scope

The $0 is currently selected item in chrome DOM inspector. $1, $2 .. and so on are previously selected items.

share|improve this answer

Inspect the element, then use this in the console

s = $($0).scope()
// `s` is the scope object if it exists
share|improve this answer

Say you want to access the scope of the element like

<div ng-controller="hw"></div>

You could use the following in the console:

angular.element(document.querySelector('[ng-controller=hw]')).scope();

This will give you the scope at that element.

share|improve this answer
1  
we don't need "document.querySelector" here – STEVER May 20 '15 at 14:04

Just define a JavaScript variable outside the scope and assign it to your scope in your controller:

var myScope;
...
app.controller('myController', function ($scope,log) {
     myScope = $scope;
     ...

That's it! It should work in all browsers (tested at least in Chrome and Mozilla).

It is working, and I'm using this method.

share|improve this answer
1  
Using global variables is a bad practice, but I guess this is okay in most cases. It's only for debugging after all; But still you have to be careful not to use the same variable name twice. – Pedro Affonso Aug 29 '15 at 12:53
    
It's a bad idea because it requires you to modify the source code. This is annoying even if it's your own code, and impossible if it's something running on another server. Even if you can modify the code, you then have to remember to undo it. So while it may work, it is not the best practice. – Jim Davis Oct 6 '15 at 16:49

protected by Pankaj Parkar Nov 11 '15 at 19:10

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.