Call Function, $scope, Model of One Page From/on Second Page in AngularJS

Introduction

In one of my previous article I explained "How to Call $scope, Model of Child Page on Parent Page in AngularJs". In that article I showed how to use "$parent" to make the scope available at the parent page.

In that article I also explained that their are other ways to call the scope value on other pages. This article will show you one more way of using the scope value on another page and one more thing is included in this article, that is you can also call the function created on another JavaScript page by some other page. This will be done using the $rootScope of AngularJs.

AngularJs provides a very helpful feature that can be used to call $scope values, functions of one page using some other page. This feature helps greatly in those cases where you want to access the $scope value of various pages on one page or vice versa.

Let's create a sample project where I can elaborate upon both of these features.

Step 1

First of all I created a HTML page. In this page I created a div to call the controller. Under this div I used an h1 tag, a button and one more div that will be used to include our child page, so the code is something as in the following:

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4.   <head>  
  5.     <script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>  
  6.     <link href="style.css" rel="stylesheet" />  
  7.     <script src="script.js"></script>  
  8.     <script src="testRootScript.js"></script>  
  9.   </head>  
  10.   
  11.   <body ng-app>  
  12.       <h3>  
  13.           I am First Page  
  14.       </h3>  
  15.       <hr />  
  16.     <div ng-controller="TestCtrl">  
  17.       <h1>{{value}}</h1>  
  18.       <div ng-include="'test.html'"></div>  
  19.       <button ng-click="fetch()">Go!!</button>  
  20.     </div>  
  21.   </body>  
  22. </html> 

Step 2

After this I created another page and on this page again I created a div that is used to call another controller, so both pages have their own controllers. In this div a simple Text Box is used whose value will be used in the model and this model will be used in the first page.

  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>  
  6.     <link href="style.css" rel="stylesheet" />  
  7.     <script src="testRootScript.js"></script>  
  8.     <script src="script.js"></script>  
  9. </head>  
  10.   
  11. <body ng-app>  
  12.     <hr />  
  13.     <h4>  
  14.         I am on Second Page  
  15.     </h4>  
  16.     <div ng-controller="TestRootCtrl">  
  17.         <input type="text" placeholder="Enter your name" ng-model="value">  
  18.     </div>  
  19.     <hr />  
  20. </body>  
  21. </html> 

Step 3

Now I am working on the Angular part of the first page.

Add a JavaScript page and on that page add the Angular code as in the following code snippet:

  1. var TestCtrl = function ($scope, $rootScope) {  
  2.     $scope.value = $rootScope.value;  
  3.     $scope.fetch = function () {  
  4.         $rootScope.callFunction();  
  5.         $scope.value = $rootScope.value;  
  6.     };  
  7. }; 

Here I created the controller with the name "TestCtrl", you can see that along with $scope, $rootScope is also available. $rootScope is the parent of all the scopes. All pages can access the $rootScope value.

Here I have created an object named as value in which the $rootScope value is passed. Also in the next function I had called a function that is also defined using $rootScope. These functions and objects are not on this page but lies somewhere else and still I will be able to use it's value and call the function.

Step 4

Now I am adding one more JavaScript page where the second controller will be defined and all the preceding rootscope values will also exist.

  1. var TestRootCtrl = function ($scope, $rootScope) {  
  2.     $rootScope.callFunction = function () {  
  3.         alert("Function Called Successfully");  
  4.         $rootScope.value = "Anubhav Chaudhary";  
  5.     };  
  6. }; 

Here I have created the controller with the name "TestRootCtrl", this controller is used on the second page.

Here you can see that I created a function named as "callFunction", this function is defined using rootScope so it can be called from other pages.

Since the function will be called, we will get to understand since an alert message will be shown. We will also see the value of the rootScope object in all those places where we have bound it or passed it in some other scope object.

One major thing to keep in mind and to check is that the path of both of the script pages are passed on both the HTML pages otherwise your rootScope value will not be accessed and an error will be thrown.

  1. <head>  
  2.   <script src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>  
  3.   <link href="style.css" rel="stylesheet" />  
  4.   <script src="script.js"></script>  
  5.   <script src="testRootScript.js"></script>  
  6. </head> 

Output

On running the application you will see that the second page is coming along with the first page as it's child, since it was called using the ng-include directive on the first page.

rootScope in AngularJS

As you click on the button first, an alert box will be shown that will confirm that our function is hit.

rootScope in AngularJS

And as you close the window, the value of the rootScope object will pass into the control that were bound to this object.

rootScope in AngularJS


Similar Articles