Easy learning with example program codes

Angularjs rootscope tutorial

Angularjs rootscope:

The $rootScope is the parent scope object for an AngularJS application and it will be single for an application. The data and methods of $rootScope object will be available to all the controllers. All $scope objects are child objects.

Example Explanation:

First include the AngularJS library in the application. The ng-app directive initializes the application. The ng-controller directive defines the controller. We defined three controllers here and set their properties using $scope object. The parentController in the root scope for our application and its data and methods will be available for all scope objects. As we can see that siblingController is not its child controller but can access its data.


<!DOCTYPE html>
 <script src="">
<body ng-app="testApp">
<h1>AngularJS RootScope Example</h1>
    <div ng-controller="parentController">
        Controller Name: {{controllerName}} <br/>
        Message: {{message}} <br/><br/>
        <div ng-controller="childController">
            Controller Name: {{controllerName}} <br/>
            Message: {{message}} <br/><br/>
    <div  ng-controller="siblingController">
        Controller Name: {{controllerName}} <br/>
        Message: {{message}} <br/>
        var ngApp = angular.module('testApp', []);
        ngApp.controller('parentController', function ($scope, $rootScope) {
            $scope.controllerName = "parentController";
            $rootScope.message = "Hello World!";
        ngApp.controller('childController', function ($scope) {
            $scope.controllerName = "childController";
        ngApp.controller('siblingController', function ($scope) {
			$scope.controllerName = "siblingController";

Try it:

JS Bin on

Copyright © 2018 CodesJava Protection Status SiteMap