All the $scopes of an AngularJS application are children of the $rootscope. An app can have only one $rootScope. It is the scope that is created on the HTML element that contains the ng-app directive and is available in the entire application.
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.
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.
The $scope is glue between a controller and view (HTML). It transfers data from the controller to view and vice-versa. As we have seen in the controller section, we can attach properties and methods to the $scope object inside controller function.
"$rootScope” is a parent object of all “$scope” angular objects created in a web page.
$scope is created with ng-controller
while $rootscope is created with ng-app
.
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.
Example: If in the example below you replace $rootScope
with $scope
the department property will not be populated from the first controller in the second one
angular.module('example', [])
.controller('GreetController', ['$scope', '$rootScope',
function($scope, $rootScope) {
$scope.name = 'World';
$rootScope.department = 'Angular';
}
])
.controller('ListController', ['$scope',
function($scope) {
$scope.names = ['Igor', 'Misko', 'Vojta'];
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="example">
<div class="show-scope-demo">
<div ng-controller="GreetController">
Hello {{name}}!
</div>
<div ng-controller="ListController">
<ol>
<li ng-repeat="name in names">{{name}} from {{department}}</li>
</ol>
</div>
</div>
</body>
According to Angular's Developer's Guide to Scopes:
Each Angular application has exactly one root scope, but may have several child scopes. The application can have multiple scopes, because some directives create new child scopes (refer to directive documentation to see which directives create new scopes). When new scopes are created, they are added as children of their parent scope. This creates a tree structure which parallels the DOM where they're attached.
Both controllers and directives have reference to the scope, but not to each other. This arrangement isolates the controller from the directive as well as from DOM. This is an important point since it makes the controllers view agnostic, which greatly improves the testing story of the applications.
$rootScope
is available globally, no matter what controller you are in, whereas $scope
is only available to the current controller and it's children.
In other way we can look at this; $rootScope
is global while $scope
is local. When Controller
is assigned to a page, so a $scope
variable can be use here because it binds to this controller. But when we want to share its value across to other controllers or services, then $rootScope
is being used (**there are alternative ways, we can share values across but in this case we want to use $rootScope
).
Your second question about how you define those two words are correct.
Lastly a bit off track, please use $rootScope
with care. Similar to the way you use global variables, can be a pain to debug and you may accidentally change the global variable somewhere inside a timer or something which makes your reading incorrect.
Every application has atleast one single rootScope and its lifecycle is the same as the app and every controller can have it's own scope, that is not shared with others.
Have a look at this article :
https://github.com/angular/angular.js/wiki/Understanding-Scopes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With