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.
I would like to access my I can neither see |
||||
Pick an element in the HTML panel of the developer tools and type this in the console:
In WebKit, You can also target the scope by element ID, like so:
Addons/Extensions There are some very useful Chrome extensions that you might want to check out:
Playing with jsFiddle When working with jsfiddle you can open the fiddle in show mode by adding http://jsfiddle.net/jaimem/Yatbt/show jQuery Lite If you load jQuery before AngularJS,
Of a button
... and so on. You might actually want to use a global function to make it easier:
Now you could do this
Check here: http://jsfiddle.net/jaimem/DvRaR/1/show/ |
|||||||||||||||||||||
|
To improve on jm's answer...
Or if you're using jQuery, this does the same thing...
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
|
|||||||||
|
If you have installed Batarang Then you can just write:
when you have the element selected in the elements view in chrome. Ref - https://github.com/angular/angularjs-batarang#console |
|||||||||||||
|
This is a way of getting at scope without Batarang, you can do:
Or if you want to find your scope by controller name, do this:
After you make changes to your model, you'll need to apply the changes to the DOM by calling:
|
|||||||||||||||||||||
|
Somewhere in your controller (often the last line is a good place), put
If you want to see an inner/implicit scope, say inside an ng-repeat, something like this will work.
Then in your controller
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,
|
|||||||||
|
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 For example, if your controller directive is created like so:
|
|||
|
I agree the best is Batarang with it's Also, if like me you have you main scope on the |
|||
|
I usually use jQuery data() function for that:
The $0 is currently selected item in chrome DOM inspector. $1, $2 .. and so on are previously selected items. |
|||
|
Inspect the element, then use this in the console
|
|||
|
Say you want to access the scope of the element like
You could use the following in the console:
This will give you the scope at that element. |
|||||
|
Just define a JavaScript variable outside the scope and assign it to your scope in your controller:
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. |
|||||||||
|
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?
window.MY_SCOPE = $scope;
first thing in my controller function. – Jason Goemaat May 31 '13 at 1:23$scope
objects of selected DOM elements into Firebug's DOM Inspector. – Kos Prov Jan 15 '14 at 14:13