What is scope in AngularJS ?
Last Updated :
28 Jul, 2022
In this article, we will see what the scope is in AngularJS and how to use Scope, along with understanding its implementation through the examples.
The Scope in AngularJS is the binding part between HTML (view) and JavaScript (controller) and it is a built-in object. It contains application data and objects. It is available for both the view and the controller. It is an object with available properties and methods. There are two types of scopes in Angular JS.
How to use the Scope?
When we make a controller in AngularJS, we will pass the $scope object as an argument. In AngularJS, it creates and injects a different $scope object into each controller in an application. Thus, the data and methods attached to $scope inside one controller cannot be accessed on another controller. Amidst the nested controller, the child controller will acquire the parent controller's scope object. Accordingly, the child controller can access properties added to the parent controller but the parent controller cannot access properties annexed to the child controller.
Understanding the Scope: If we see an AngularJS application that consists of:
- The HTML view.
- Model, the data which is available for the current view.
- Controller, the JavaScript function that makes/changes/removes/controls the data.
The scope is the model and it is a JavaScript object with properties and methods which are available for both the view and the controller.
Example 1: In this example, we will see the use of the $scope object & will also see how data is transferred from the controller to the view component which is rendered by using interpolation.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body>
<div ng-app="gfg"
ng-controller="myCtrl">
<h1>{{organisation}}</h1>
</div>
<p>
The property "organization" was made in
the controller, and can be referred to
in the view by using the {{ }} brackets.
</p>
<script>
var geeks = angular.module("gfg", []);
geeks.controller("myCtrl", function($scope) {
$scope.organisation = "GeeksforGeeks";
});
</script>
</body>
</html>
Output:
Properties made by the controller can be referred to in the view.Example 2: From the previous example, we have used only single scope, but for larger applications, which may be a section in the HTML DOM that can access certain scopes.
HTML
<!DOCTYPE html>
<html>
<head>
<title>AngularJS Scope</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</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 = [
"DSA Learning",
"Algorithm Learning",
"Web Technology",
];
});
</script>
</body>
</html>
Output:
In the above example, there is only one scope in the example below, you will see more than one scope.Root Scope: Root scope is a scope that is created on the HTML element which has the ng-app directive & is contained in all the applications. The availability of root scope is in the whole application.
Example 3: If any variable is declared with the same name in the current scope & root scope then the current scope will be used by the application. In this example, we will see how the variable named "color" is used by the current scope.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
</head>
<body ng-app="myApp">
<p>The rootScope's favorite color:</p>
<h1>{{color}}</h1>
<div ng-controller="myCtrl">
<p>The scope of the controller's favorite color:</p>
<h1>{{color}}</h1>
</div>
<p>The rootScope's favorite color is still:</p>
<h1>{{color}}</h1>
<p>
Note that the color variable in controller does
not overwrite the color value in rootScope.
</p>
<script>
var app = angular.module("myApp", []);
app.run(function($rootScope) {
$rootScope.color = "turquoise";
});
app.controller("myCtrl", function($scope) {
$scope.color = "salmon";
});
</script>
</body>
</html>
Output:
A variable named "color" exists in both the controller's scope and the rootScope.
Difference between $Scope & $rootScope:
$rootScope | $Scope |
---|
$rootScope is a parent object of all "$scope" angular objects created in a webpage.
| $scope is a child object that is used to bind the HTML(view) & Javascript(Controller) in a webpage.
|
It is created with the ng-app directive.
| It is created with the ng-controller directive.
|
It is available globally for all the controllers, i.e, the property assigned with "$rootscope" can be used anywhere.
| It is available only to the controller that has created it, i.e. the property assigned with "$scope" can't be used outside the controller in which it is defined.
|
In this case, every application has at least a single "$rootscope" & its lifecycle is the same as the app.
| In this case, every controller can have its own scope, which is not shared with others.
|
Similar Reads
What is View in AngularJS ? In AngularJS, the View is what the user can see on the webpage. In an application (as per the userâs choice), the chosen view is displayed. For this purpose, the ng-view directive is used, which allows the application to render the view. Any changes made in the view section of the application are re
7 min read
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 AngularJS. $Scope$rootScope Scope: There is few specific
2 min read
What is Angular ? Angular is an open-source web application framework maintained by Google and a community of developers. It is designed to build dynamic and interactive single-page applications (SPAs) efficiently. With Angular, developers can create robust, scalable, and maintainable web applications. Table of Conte
5 min read
What are templates in AngularJS ? Templates in AngularJS are simply HTML files filled or enriched with AngularJS stuff like attributes and directives. A directive is a marker element that is used to target a particular attribute or class to render its behavior according to the needs. Model and controller in Angular are combined with
3 min read
What is the AppModule in Angular ? In Angular, AppModule plays an important role as the entry point to an Angular application. In this article, we'll learn about what AppModule is, its structure, and its significance in Angular applications. We'll also look at some examples to have a clear understanding. Table of Content What is AppM
4 min read
What are the AngularJs Global API ? Global API in AngularJS: API stands for Application Programming Interface. It is a set of protocols, routines, and tools for building software applications that allow the user to interact with the application and perform several tasks. In AngularJS Global API is a set of global Javascript functions
3 min read
Explain AngularJS Scope Life-Cycle In AngularJS, The Scope acts as a merging parameter between the HTML and Javascript code, ie, it is the binding part between view and controller and it is a built-in object. It is available for both the view and the controller. It is used to define inside the controller, in order to define the membe
8 min read
What is the role of $routeProvider in AngularJS ? In this article, we will see the role of the $routeProvider in AngularJS, along with understanding the basic implementation through the examples. Routing allows us to create Single Page Applications. To do this, we use ng-view and ng-template directives, and $routeProvider services. We use $routePro
3 min read
What are Directives in AngularJS ? AngularJS directives are extended HTML attributes having the prefix ng-. Directives are markers on the DOM element which tell Angular JS to attach a specified behavior to that DOM element or even transform the DOM element with its children. During compilation, the HTML compiler traverses the DOM mat
7 min read
How to use $scope.$apply() in AngularJS ? In this article, we will be discussing the $apply() function & how to use it in angularjs. In AngularJS, $apply() function is used to evaluate expressions outside of the AngularJS context (browser DOM Events, XHR). Moreover, $apply has $digest under its hood, which is ultimately called whenever
3 min read