Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding scope inheritance in AngularJS

  Author : Shailendra Chauhan
Posted On : 29 Dec 2014
Total Views : 7,858   
Updated On : 26 Sep 2016
 

The $scope object used by views in AngularJS are organized into a hierarchy. There is a root scope, and the $rootScope can has one or more child scopes. Each controller has its own $scope (which is a child of the $rootScope), so whatever variables you create on $scope within controller, these variables are accessible by the view based on this controller.

For example, suppose you have two controllers: ParentController and ChildController as given below:

<!DOCTYPE html>
<html>
<head>
 <title>Angular Scope Inheritance</title>
 <script src="lib/angular.js"></script>
 <script>
 var app = angular.module('ScopeChain', []);

 app.controller("parentController", function ($scope) {
 $scope.managerName = 'Shailendra Chauhan';
 $scope.$parent.companyName = 'Dot Net Tricks'; //attached to $rootScope
 });
 app.controller("childController", function ($scope, $controller) {
 $scope.teamLeadName = 'Deepak Chauhan';
 });
 </script>
</head>
<body ng-app="ScopeChain">
 <div ng-controller="parentController ">
 <table style="border:2px solid #e37112">
 <caption>Parent Controller</caption>
 <tr>
 <td>Manager Name</td>
 <td>{{managerName}}</td>
 </tr>
 <tr>
 <td>Company Name</td>
 <td>{{companyName}}</td>
 </tr>
 <tr>
 <td>
 <table ng-controller="childController" style="border:2px solid #428bca">
 <caption>Child Controller</caption>
 <tr>
 <td>Team Lead Name</td>
 <td>{{ teamLeadName }}</td>
 </tr>
 <tr>
 <td>Reporting To</td>
 <td>{{managerName}}</td>
 </tr>
 <tr>
 <td>Company Name</td>
 <td>{{companyName}}</td>
 </tr>
 </table>
 </td>
 </tr>
 </table>
 </div>
</body>
</html>

How it works...

What do you think?

I hope you will enjoy the AngularJS scope hierarchy while developing your app with AngularJS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (0)
23 MAY
Xamarin Forms : Build Cross-platform Apps (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
5 MAY
ASP.NET MVC with Angular4 Development (Online)

06:30 AM-08:30 AM IST(+5.30 GMT)

Know More
29 APR
Xamarin Forms : Build Cross-platform Apps (Classroom)

5:00 PM-6:30 PM

Know More
29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

Know More
27 APR
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

Know More
22 APR
NodeJS Development (Classroom)

11:00 AM-12:30 PM

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

8 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

03:30 PM-05:30 PM

24 FEB
Angular 2 with Type Script (Online)

09:00 PM-11:00 PM IST(Mon,Wed,Fri)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+