Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access $scope variable in angular from chrome console

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!

like image 828
zotherstupidguy Avatar asked Mar 27 '13 15:03

zotherstupidguy


People also ask

How do you access $scope in console?

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 .

How does $scope work in angular?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

Is $scope still supported in angular2?

In Angular 2.0, there will be no $scope .

What is the difference between $scope and scope in angular?

The $ in "$scope" indicates that the scope value is being injected into the current context. $scope is a service provided by $scopeProvider . You can inject it into controllers, directives or other services using Angular's built-in dependency injector: module.


2 Answers

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(); 
like image 185
BraveNewMath Avatar answered Sep 30 '22 11:09

BraveNewMath


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?

like image 27
Will Avatar answered Sep 30 '22 13:09

Will