Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $scope.$root and $rootScope?

I see in controllers that $scope has $root, what is this? How is it different from $rootScope which can be injected in the controller?

like image 504
Rishul Matta Avatar asked Mar 06 '14 06:03

Rishul Matta


People also ask

What is scope and rootScope in angular?

Root ScopeAll 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.

What is the use of dollar rootScope?

$scope is a JavaScript object that refers to the application data. It has properties and methods attached to it that are available for both the view and the controller. So, we can say that $scope is the binding part between the HTML view and the JavaScript controller.

What is the difference between $scope and scope?

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.

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.


2 Answers

$rootScope var which points to the parent of all the scopes and can be injected everywhere. All other scopes are children of the $rootScope. They are created via the $new method of the $rootScope thus every scope inherits from the $rootScope.

In the angular source in the definition of the Scope constructor there is a line :

 function Scope() {    this.$id = nextUid();  ...  this['this'] = this.$root =  this;  ... 

It seems the $root var is just a placeholder for this of the first scope created - $rootScope.

Next there is this piece of code in the $new method:

  $new: function(isolate) {       ...      if (isolate) {       child = new Scope();       child.$root = this.$root;    ...    return child; 

So the $root var of every scope child of $rootScope is a reference to $rootScope. And all the children of those children will get the same reference to $rootScope

In my opinion it is better to use the $rootScope via dependency injection because it is an explicit and overall more frequently used way of referring to the $rootScope

like image 199
Igor Malyk Avatar answered Oct 03 '22 14:10

Igor Malyk


As mentioned before, $scope.$root holds a reference to the $rootScope.

Unfortunately, there IS a difference between using $scope.$root and using $rootScope:

  1. When $scope IS the root, its $root property is null
  2. $scope.$root is only assigned on isolate scopes: https://github.com/angular/angular.js/blob/v1.3.6/src/ng/rootScope.js#L204

So you might have a situation where $scope.$root is null. Better use $rootScope instead...

like image 42
Danny R Avatar answered Oct 03 '22 14:10

Danny R