Wednesday, July 22, 2015

Controller to Controller Communication Using $RouteScope in AngularJS

Here I've merely thought to write my thoughts and hands-on experience with Angular. This is another article that tells you how to get your hands dirty with Controller to Controller communication using $RouteScope in AngularJs AngularJs.
I've designed a program to help you become familiar with just a few keywords that you may be confronting in the future. Those keywords are shown below in the article.
As a newbie to Angular you must be familiar with a few main keywords that you will be using in an application:

  • ng-app: This directive ng-app tells that this element is the "owner" of an AngularJs application.
  • ng-model: This directive ng-model binds the value of the input field to the application variablefirstName.
  • ng-bind: This directive ng-bind binds the innerHTML of the specific element to the application variable firstName.

The Application Life Cycle of this program is shown below:

Life cycle of Basic application

I've created an HTML page as the View and named it FirstAngularApp.html that keeps a reference of an Angular.min.js file. You can download and add it to the solution as well.

  1: Here I have a HTML page that keeps the following code segment.
  3: <!DOCTYPE html>  
  4: <html>  
  5:     <head>  
  6:         <title></title>  
  7:         <script src=""></script>  
  8:         <script type="text/javascript" src="Scripts/Main1.js"></script>  
  9:         <!--<script src="Scripts/Main1.js"></script>-->  
 10:         <script></script>  
 11:     </head>  
 12:     <body ng-app="myApp" ng-controller="Myctrl">  
 13:         <div ng-controller="Myctrl1">  
 14:             <strong>Custom information :</strong> {{message}}  
 15:             <br />  
 16:             <strong>Custom information provide by Controller 2 is :</strong> {{ CompName(message) }}  
 17:             <br />  
 18:         </div>  
 19:         <!--   
 20:     <div ng-controller="Myctrl3"></div> -->  
 21:     </body>  
 22: </html> 

Please have a look at the image shown below:
In the code segment shown above it has <strong>{{ CompName(message) }} <br /><br />. This code calls the function CompName defined in the Controller Myctrl1. As soon as this line executes it goes to the controller and performs the required actions.
Please refer to the image shown below that keeps the code segment for the Main1.js file.


The $rootScope is the top-most scope and acts like a global variable. An app can have only one $rootScope that will be shared among all the components. All other $scopes are children of the $rootScope.
Code segment for main1.js

  1: /// <reference path="angular.intellisense.js" />
  2: /// <reference path="angular.js" />
  3: /// <reference path="angular.min.js" />
  6: var app = angular.module('myApp', []);  
  8: app.controller('Myctrl', function ($scope, $rootScope) {  
  9: //debugger;
 10:     $scope.firstName = "Sachin Kalia";  
 11:     $scope.lastName = "";  
 14:     $rootScope.testProp = "sachin";  
 16:     $rootScope.CompDetail2 = function CompDetail2(data) {  
 17: return "I am called from Controller1 RootScope(Global) method name CompDetail2  =>=> " + data;  
 19:     };  
 21:     $scope.CompDetails = function CompDetails() {  
 22: return "Welcome to " + $scope.firstName + " " + $scope.lastName;  
 24:     };  
 25: });  
 28: app.controller('Myctrl1', function ($scope, $rootScope) {  
 29:     alert("Welcome");  
 30:     $scope.message = " Welcome to!!!";  
 31:     $scope.CompName = function CompName(data) {  
 33: return $rootScope.CompDetail2(data);  
 35: //alert(returnValue + "i Have added this value ");
 36: // $rootScope.CompDetail(data);
 37: // alert("passing value to next controller" + data);
 38:     };  
 39: });  

As soon as you run BasicAngularApp.html it shows you the result as shown below in the image.
custom information
If you noticed from the image, it has main1.js shown above that calls the method compName() that accepts data as the parameter sent by the view page. Further it again tries to access the method CompDetail2() that is already defined in another controller named Myctrl and accessible using the $routeScope global variable.
$routeScope methods and property should be defined/written prior to the calling method otherwise it will provide an “undefined” error since JavaScript finds the reference line by line.
Kindly have a look at the Life cycle of the basic application as depicted below:
Life cycle of Basic application
I hope it'll help you some day.
You can download a sample application from here : Controller to Controller Communication Using $RouteScope in AngularJS

Kindly inform me if having any query.

Cheers, .Net
Sachin Kalia


  1. awesome one.. this explains $scope & $rootscope in depth.. u covered it all..

  2. Very good information on communicating with $RouteScope in AngularJS.

    AngularJS Training