Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

how to access the scope variable widgets from chrome's console

function MyCntrl($scope) {
      $scope.widgets = [
                  {text:'Widget #1', datarow:1, datacol:1, datasizex:3, datasizey:3},
                  {text:'Widget #2', datarow:2, datacol:1, datasizex:3, datasizey:3},
                  {text:'Widget #3', datarow:1, datacol:2, datasizex:3, datasizey:3},
                  {text:'Widget #4', datarow:2, datacol:2, datasizex:3, datasizey:3}
      ];

something like $scope.widgets simply doesnt work in the console!

share|improve this question
2  
possible duplicate of how to access the angular $scope variable in browsers console – beauXjames May 29 '14 at 16:21
up vote 23 down vote accepted

The scope is bound to the DOM so you need to grab an element and use some angular code to get the scope.

Your best bet is to get an element that the Controller is bound to and have a look at the scope on that.

Here is the answer how to access the angular $scope variable in browsers console

share|improve this answer
6  
Thanks angular.element('[ng-controller=MyCntrl]').scope() did the trick – babydudecoder Mar 27 '13 at 16:13
7  
Make sure to call angular.element('[ng-controller=MyCntrl]').scope().$apply(); if you modify any scope variables in the console this way. Otherwise you won't see the changes. – GFoley83 Jul 17 '13 at 1:18

You can either follow the asnwer of Will or install Angular Batarang Chrome extension. This will not only allow you to view and manipulate '$scope' object from, let's say your JavaScript console, but also it's a fundamental tool when developing complex AngularJS apps.

share|improve this answer

this is a way of getting at scope without batarang. Assuming you have references to jquery and angular on your page, 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
1  
This becomes even more powerful if you use the magical builtin element selector. Select the element in the dom tree, then evaluate angular.element($0).scope() in the console – LOAS Nov 3 '15 at 8:05

Aside from the traditional console based commands. I also find this chrome plugin very handy. Simply select the element you want to inspect and switch over to the plugin to see the scope/vars for that scope.

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

share|improve this answer

Here is one Example: I have following DOM on my angular Single Page application:

<div ng-controller="usersAppController as uac" class="ng-scope">
    <div class="tab ng-scope is-normal is-active" id="rolesTab" ref="tabs" mode="normal" target-ref="rolesContent" xng-locals="xng.userApp.roles">Roles
    </div>
</div>

and this div is present in the controller name usersAppController as uac Above here I have controller as syntax

Also note down that I am using jQuery in my application:

$($0).controller() method will give me a direct access to the uac object which is on the $scope object.

$($0).scope() will give me prototype chain for the element I have selected and if I follow that chain I will find my scope object on that element.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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