top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What does $rootScope mean in AngularJs?

+1 vote
450 views
What does $rootScope mean in AngularJs?
posted Jan 5, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

$rootScope which is the scope created on the HTML element that contains the ng-app directive

Every application has a single root scope. All other scopes are descendant scopes of the root scope. Scopes provide separation between the model and the view, via a mechanism for watching the model for changes. They also provide event emission/broadcast and subscription facility.

answer Jan 7, 2017 by Arun Angadi
0 votes

A scope provides a separation between View and its Model. Every application has a $rootScope provided by AngularJS and every other scope is its child scope.

Using $Rootscope

Using rootscope we can set the value in one controller and read it from the other controller.

enter image description here

<!DOCTYPE html>  
<html ng-app="myApp">  

<head>  
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js">  
    </script>  
    <script>  
        var myApp = angular.module('myApp', []);  

        function controllerOne($scope, $rootScope)  
        {  
            $rootScope.name = 'From Rootscope set in controllerOne';  
        }  

        function controllerTwo($scope, $rootScope)   
        {  
            $scope.name2 = $rootScope.name;  
        }  
    </script>  
</head>  

<body>  
    <div style="border: 5px solid gray; width: 300px;">  
        <div ng-controller="controllerOne">  
            Setting the rootscope in controllerOne  
        </div>  
        <div ng-controller="controllerTwo">  
            Hello, {{name2}}!  
        </div>  
        <br />  
    </div>  
</body>  

</html>  

As we know, Rootscope is the top-level data container in AngularJs, we can keep any data in rootscope and read it when needed.

For more details

answer Aug 1, 2017 by Jdk
...