What is Scope and Scope Inheritance


What is scope - Scope is plain java script object. Scope is glue between view and controller. A controller is responsible for setting of model and functions. Model define in scope are accessible on view (html). Its work in two-way bindings. when model updated on scope it automatically refresh on view that's way its also called two-way binding.

Scope Inheritance - AngularJs Scope Inheritance work similar to JavaScript Inheritance. By default Child scope inherit parent scope. scope can be nested. Scope inheritance used for creating reusable component directive.

Code - http://plnkr.co/edit/wBAtJ8?p=preview








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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<html>
 
<head>
  <title>What is Scope and Scope Inheritance?</title>
  <script>
    var myApp = angular.module("myScopeApp", []);
 
    myApp.controller("LocationController", function($scope) {
      $scope.Country = 'India';
      $scope.Location = 'Mumbai';
 
    });
 
    myApp.controller("ProfileController", function($scope) {
 
      $scope.Name = 'Sachin';
      $scope.Age = 10;
      $scope.Location = 'Navi Mumbai';
 
    });
 
    myApp.controller("JobController", function($scope) {
 
      $scope.Job = 'Software Engineer';
      $scope.Company = 'Company A';
      $scope.Location = 'Noida';
 
    });
  </script>
</head>
 
<body ng-app="myScopeApp">
  <h2>Scope Inheritance?</h2>
  <div ng-controller="LocationController">
    <strong> User Profile </strong>
    <p>{{Country}} - {{Location}}</p>
 
    <div ng-controller="ProfileController">
      <p><strong> Name </strong >- {{Name}}
        <br/><strong> Age </strong>- {{Age}}
        <br/><strong>Location </strong> - {{Location}}
        <!-- local scope-->
 
        , {{$parent.Location}}
        <!--  inherit from parent scope -->
 
        , {{Country }}
        <!--  country not avaliable in local scope then
it will inherit from parent scope -->
 
    </div>
 
    <div ng-controller="JobController">
      <p><strong> Job </strong> - {{Job}} , {{Location}} ,
        <!-- local scope-->
        {{Country}}
        <!-- local scope-->
        <br/>
      </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 myApp = angular.module("myScopeApp", []);

    myApp.controller("LocationController", function($scope) {
      $scope.Country = 'India';
      $scope.Location = 'Mumbai';

    });

    myApp.controller("ProfileController", function($scope) {

      $scope.Name = 'Sachin';
      $scope.Age = 10;
      $scope.Location = 'Navi Mumbai';

    });

    myApp.controller("JobController", function($scope) {

      $scope.Job = 'Software Engineer';
      $scope.Company = 'Company A';
      $scope.Location = 'Noida';

    });
  </script>
</head>

<body ng-app="myScopeApp">
  <h2>Scope Inheritance?</h2>
  <div ng-controller="LocationController">
    <strong> User Profile </strong>
    <p>{{Country}} - {{Location}}</p>

    <div ng-controller="ProfileController">
      <p><strong> Name </strong >- {{Name}}
        <br/><strong> Age </strong>- {{Age}}
        <br/><strong>Location </strong> - {{Location}}
        <!-- local scope-->

        , {{$parent.Location}}
        <!--  inherit from parent scope -->

        , {{Country }}
        <!--  country not avaliable in local scope then 
it will inherit from parent scope -->

    </div>

    <div ng-controller="JobController">
      <p><strong> Job </strong> - {{Job}} , {{Location}} ,
        <!-- local scope-->
        {{Country}}
        <!-- local scope-->
        <br/>
      </p>
    </div>

  </div>
</body>

</html>

Comments