How to access $scope variable in angular from chrome console
Asked Answered
W

5

48

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 doesn't work in the console!

Wag answered 27/3, 2013 at 15:59 Comment(1)
possible duplicate of how to access the angular $scope variable in browsers consoleTakeover
P
33

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 do I access the $scope variable in browser's console using AngularJS?

Pert answered 27/3, 2013 at 16:7 Comment(2)
Thanks angular.element('[ng-controller=MyCntrl]').scope() did the trickWag
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.Cumulative
L
35

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();
Lipoma answered 4/6, 2014 at 17:4 Comment(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 consoleKippar
P
33

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 do I access the $scope variable in browser's console using AngularJS?

Pert answered 27/3, 2013 at 16:7 Comment(2)
Thanks angular.element('[ng-controller=MyCntrl]').scope() did the trickWag
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.Cumulative
S
17

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.

Sall answered 27/3, 2013 at 17:56 Comment(0)
C
4

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.

Curry answered 15/1, 2016 at 14:51 Comment(0)
C
3

You can first select an element from the DOM that's within the scope you want to inspect:

enter image description here

Then you can view the scope object by querying the following in the console:

angular.element($0).scope()

You can query any property on the scope, e.g.:

angular.element($0).scope().widgets

Or you can inspect the controller attached to the scope:

angular.element($0).scope().$myControllerName

Camisado answered 30/9, 2021 at 5:24 Comment(1)
Great answer. More details here docs.angularjs.org/guide/scope in Retrieving Scopes from the DOM sectionPneumograph

© 2022 - 2024 — McMap. All rights reserved.