AngularJS | Scope

Scope in AngularJS is the binding part of HTML view and JavaScript controller. When you add properties into the scope object in the JavaScript controller, only then the HTML view gets access to those properties. There are two types of Scope in AngulerJS.

  • $Scope
  • $rootScope

Scope: There is few specific features in Scope those are listed below

  • The HTML view
  • The data which is available for the current view known as Model
  • The JavaScript function that makes/changes/removes/controls the data known as Controller.

Syntax:



$scope

Example 1: This example will illustrate the scope concept more clearly this example contain single scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
      AngularJS | Scope
    </title>
    <script src=
    </script>
</head>
  
<body>
  
    <div ng-app="gfg" ng-controller="control" align="center">
  
        <h1 style="color:green;">{{organization}}</h1>
        <p>A Computer Science Portal</p>
    </div>
  
    <script>
        var geeks = angular.module('gfg', []);
        geeks.controller('control', function($scope) {
            $scope.organization = "GeeksforGeeks";
        });
    </script>
  
</body>
  
</html>

chevron_right


Output:

Example 2: In the above example there is only one scope in the below example you will see more than one scope.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        AngularJS | Scope
    </title>
    <script src=
    </script>
</head>
  
<body>
  
    <div ng-app="gfg" ng-controller="control">
  
        <ul>
            <li ng-repeat="x in names">{{x}}</li>
        </ul>
  
    </div>
  
    <script>
        var geeks = angular.module('gfg', []);
  
        geeks.controller('control', function($scope) {
            $scope.names = ["Python", "Machine Learning",
                               "Artificial Inteligence"];
        });
    </script>
  
</body>
  
</html>                    

chevron_right


Output:

rootScope: If your variables contains the same name in the both rootscope and current scope then the controller or the application will use the current scope.
Syntax:

$rootScope

Example 3: This example will show you what will happen if the variable name is same in controller’s scope and the rootscope.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        AngularJS | Scope
    </title>
    <script src=
    </script>
</head>
  
<body ng-app="gfg">
    <h1>GeeksforGeeks</h1>
    <p>Jack and Jones</p>
    <h3>{{relation}}</h3>
  
    <div ng-controller="control">
  
        <p>Akbar and Antony </p>
        <h3>{{relation}}</h3>
  
    </div>
  
    <p>Jay and Viru</p>
    <h3>{{relation}}</h3>
  
    <script>
        var geeks = angular.module('gfg', []);
        geeks.run(function($rootScope) {
            $rootScope.relation = 'friend';
        });
        geeks.controller('control', function($scope) {
            $scope.relation = "brothers";
        });
    </script>
  
</body>
  
</html>

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.