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 |
|||
|
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,
|
||||
|
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:
|
|||||||||||||||||||||
|
I agree the best is Batarang with it's Also, if like me you have you main scope on the |
|||
|
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 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. |
|||||
|
To add and enhance the other answers, in the console, enter If you are not using jQuery, you can use angular.element($0) as in:
To check if you have jQuery and the version, run this command in the console:
If you have inspected an element, the currently selected element is available via the command line API reference $0. Both Firebug and Chrome have this reference. However, the Chrome developer tools will make available the last five elements (or heap objects) selected through the properties named $0, $1, $2, $3, $4 using these references. The most recently selected element or object can be referenced as $0, the second most recent as $1 and so on. Here is the Command Line API reference for Firebug that lists it's references.
Some other things that you can use are:
See Tips and Tricks for Debugging Unfamiliar Angularjs Code for more details and examples. |
|||
|
Just assign
We actually need Mentioned already by @JasonGoemaat but adding it as a suitable answer to this question. |
|||
|
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 (the association bonus does not count).
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