Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to debug the $rootScope object of angularjs in the browser

Is there a way of debugging the angularjs application when it is loaded in the browser?

ie. I wish to get the $rootScope of my current application. How would I do that?

like image 925
zcaudate Avatar asked Nov 01 '12 03:11

zcaudate


People also ask

How do you debug Angular JS?

For debugging AngularJS in Chrome you can use AngularJS Batarang. (From recent reviews on the plugin it seems like AngularJS Batarang is no longer being maintained. Tested in various versions of Chrome and it does not work.) You can also use ng-inspect for debugging angular.

What is the use of $scope and $rootScope angular object?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.

How many $rootScope an AngularJS application can have *?

An app can have only one $rootScope which will be shared among all the components of an app.

What is difference between $scope and $rootScope object?

The main difference is the availability of the property assigned with the object. A property assigned with $scope cannot be used outside the controller in which it is defined whereas a property assigned with $rootScope can be used anywhere.

How to get the scope of the angular element?

angular.element(document.querySelector('[ng-controller=hw]')).scope(); This will give you the scope at that element. Share Improve this answer Follow edited Feb 25 '16 at 15:15 Mo. 23.2k3535 gold badges143143 silver badges206206 bronze badges

What happens when a scope is destroyed in angular?

Just before a scope is destroyed, a $destroyevent is broadcasted on this scope. Application code can register a $destroyevent handler that will give it a chance to perform any necessary cleanup. Note that, in AngularJS, there is also a $destroyjQuery event, which can be used to clean up DOM bindings before an element is removed from the DOM.

How do I get the root scope of a module?

- type in module ng Overview A root scope can be retrieved using the $rootScopekey from the $injector. Child scopes are created using the $new()method. (Most scopes are created automatically when compiled HTML template is executed.) See also the Scopes guidefor an in-depth introduction and usage examples. Inheritance

How do I find the root scope of an HTML template?

A root scope can be retrieved using the $rootScopekey from the $injector. Child scopes are created using the $new()method. (Most scopes are created automatically when compiled HTML template is executed.) See also the Scopes guidefor an in-depth introduction and usage examples.


4 Answers

+1 for Batarang

Also, you can get the scope from any element in the DOM by executing the following from the console

angular.element(DOMNODE).scope()

Where DOMNODE, is of course, a reference to a DOM node.

For example, in Chrome in the elements tab you can select the node where the ng-app directive is, and get the root scope with

angular.element($0).scope()
like image 163
jaime Avatar answered Oct 12 '22 07:10

jaime


In the developer console try this

$rootScope = angular.element(document).scope()
like image 30
mozey Avatar answered Oct 12 '22 07:10

mozey


Batarang -- A Google Chrome plugin for AngularJS

After you installed this, you can do

console.log($rootScope);

and check the scope object in your chrome console.

BTW, if you want to get $rootScope, you need to inject to your controller

like

app.controller('MainCtrl', function($scope, $rootScope) {

}
like image 16
maxisam Avatar answered Oct 12 '22 05:10

maxisam


In developer console type angular.element('body').scope(); in case of your app is <body data-ng-app="SomeApp">

like image 4
raghavsood33 Avatar answered Oct 12 '22 05:10

raghavsood33