Aug 3, 2015

Inheritance In Angular js

with example, step by step scope inheritance in angularjs 

 
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.
 
 
 

Scope Inheritance Example

 

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<title>AngularJS Inheritance</title>
</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>
<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>
</body>
</html>

 
 

No comments:

Post a Comment