angularjs Scope Inheritance

What are scope and scope inheritance?

Scope :  Click for live demo

The scope is an object of JavaScript. The scope is provide the connection between controller to view. The model data contain by the scope and all the model data access by the $scope. i.e.
//assign value in scope and access it.
var parentApp = angular.module("myParentApp", []);
 parentApp.controller("MyParentController", ['$scope', function ($scope) {
    $scope.name = 'Anil Singh';
    $scope.Age = 30;
  }
}]);
//assign value in scope and access it.
var parentApp = angular.module("myParentApp", []);

 parentApp.controller("MyParentController", ['$scope', function ($scope) {
    $scope.name = 'Anil Singh';
    $scope.Age = 30;
  }
}]);

Scope Inheritance :

The Scope Inheritance work like JavaScript Inheritance. The parent scope and child scope attached with taggers.

By default, The child scope prototype inherit from the parent scope that means the child controller will inherit the parent controller and the scope can be nested and have the limit access to the properties. i.e.
var parentApp = angular.module("myParentApp", []);
parentApp.controller("MyChild1Controller", ['$scope', function ($scope) {
    $scope.child1Name = 'Child-1';
    $scope.Age = 10;
    $scope.gender = 'M';
  }
}]);
parentApp.controller("MyChild2Controller", ['$scope', function ($scope) {
    $scope.child2Name = 'Child-2';
    $scope.Age = 4;
    $scope.gender = 'F';
  }
}]);
var parentApp = angular.module("myParentApp", []);

parentApp.controller("MyChild1Controller", ['$scope', function ($scope) {
    $scope.child1Name = 'Child-1';
    $scope.Age = 10;
    $scope.gender = 'M';
  }
}]);

parentApp.controller("MyChild2Controller", ['$scope', function ($scope) {
    $scope.child2Name = 'Child-2';
    $scope.Age = 4;
    $scope.gender = 'F';
  }
}]);

The live example of scope with Inheritance
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<html>
<head>
  <title>What is Scope and Scope Inheritance?</title>
  <script>
    var parentApp = angular.module("myParentApp", []);
    parentApp.controller("MyParentController", function($scope) {
      $scope.ParentName = 'Parent';
      $scope.Age = 50;
    });
    parentApp.controller("MyChild1Controller", function($scope) {
      $scope.child1Name = 'Child-1';
      $scope.Age = 10;
      $scope.gender = 'M';
    });
    parentApp.controller("MyChild2Controller", function($scope) {
      $scope.child2Name = 'Child-2';
      $scope.Age = 4;
      $scope.gender = 'F';
    });
  </script>
</head>
<body ng-app="myParentApp">
  <h2>Scope Inheritance?</h2>
  <div ng-controller="MyParentController">
    <p>{{ParentName}}
      <br/>{{Age}}
      <div ng-controller="MyChild1Controller">
        <p>{{child1Name}}
          <br/>{{Age}}</p>
      </div>
      <div ng-controller="MyChild2Controller">
        <p>{{child2Name}}
          <br/>{{Age}}</p>
      </div>
  </div>
</body>
</html>
<html>

<head>
  <title>What is Scope and Scope Inheritance?</title>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
  <script>
    var parentApp = angular.module("myParentApp", []);

    parentApp.controller("MyParentController", function($scope) {
      $scope.ParentName = 'Parent';
      $scope.Age = 50;
    });

    parentApp.controller("MyChild1Controller", function($scope) {
      $scope.child1Name = 'Child-1';
      $scope.Age = 10;
      $scope.gender = 'M';
    });

    parentApp.controller("MyChild2Controller", function($scope) {
      $scope.child2Name = 'Child-2';
      $scope.Age = 4;
      $scope.gender = 'F';
    });
  </script>
</head>

<body ng-app="myParentApp">
  <h2>Scope Inheritance?</h2>
  <div ng-controller="MyParentController">
    <p>{{ParentName}}
      <br/>{{Age}}
      <div ng-controller="MyChild1Controller">
        <p>{{child1Name}}
          <br/>{{Age}}</p>
      </div>
      <div ng-controller="MyChild2Controller">
        <p>{{child2Name}}
          <br/>{{Age}}</p>
      </div>
  </div>
</body>

</html>

ANIL SINGH

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book