How to access the Scope from outside JS Function in AngularJS ?
Last Updated :
22 Jul, 2024
In the AngularJS application, we generally need to access the scope from the outside JS function. This can be done using the global scope object like $rootScope or the AngularJS services. Accessing the scope from the outside of JS functions is to enable the manipulation of data in the scope of the application in response to user actions. In this article, we will see how we can access the scope from outside the JavaScript function in AngularJS.
Steps for Configuring AngularJS Application
The below steps will be followed to configure the AngularJS Application:
- Create a new folder for the project. We are using the VSCode IDE to execute the command in the integrated terminal of VSCode.
mkdir access-scope
cd access-scope
- Create the index.html file in the newly created folder, we will have all our logic and styling code in this file. We can also create separate files for HTML, CSS, and JS:
We will explore the above approaches & will understand their implementation through the illustration.
Accessing Using $rootScope Object
In this approach, we are using the $rootScope object. This object refers to an object which is accessible from everywhere in the application. This object is mainly a global storage for the data which is to be shared throughout the controllers in the application. Using this object we can update the data within the scope from the functions that are not connected to the specific controller in the application. In the below example, there is the message that is set in the $rootScope, making this globally accessible throughout the application. Here, mainly we are performing the dependy injection where we are simply injecting the objects in our controller function, which includes the scope itself.
Example: Below is an example that demonstrates accessing the scope from outside the js function in AngularJS using $rootScope Object.
HTML
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>
AngularJS Scope Access
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
h1 {
color: green;
}
h3 {
color: rgb(0, 0, 0);
}
p {
font-size: 18px;
margin: 20px 0;
}
button {
background-color: #007BFF;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
</style>
</head>
<body ng-controller="myController">
<div class="container">
<h1>
GeeksforGeeks
</h1>
<h3>
Approach 1: Using $rootScope
</h3>
<p>
Message: {{ message }}
</p>
<button ng-click="updateMessage()">
Update Message
</button>
<button ng-click="resetMessage()">
Reset Message
</button>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $rootScope) {
// Set an initial message in the root scope
$rootScope.message =
"Hello from $rootScope!";
// Function to update the message in the root scope
$scope.updateMessage = function () {
$rootScope.message =
"Message updated using $rootScope!";
};
// Function to reset the message to its initial value
$scope.resetMessage = function () {
$rootScope.message =
"Hello from $rootScope!";
};
});
</script>
</body>
</html>
Output
Using AngularJS Services
In this approach, we are accessing the scope from outside the JS function by using the AngularJS services. Here, we have used the messageService which mainly wraps the message variable and allows it to be accessed from within the controller. When the user performs the edit of the message in the input field, the updateServiceMessage() function is been actioned and the message is updated using the service.
Example: Below is an example that demonstrates accessing the scope from outside the js function in AngularJS using Services.
HTML
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>
AngularJS Scope Access
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js">
</script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f0f0;
margin: 0;
padding: 0;
}
.container {
max-width: 400px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
}
h1 {
color: green;
}
h3 {
color: #333;
}
p {
font-size: 18px;
margin: 20px 0;
}
.input-field {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
</style>
</head>
<body ng-controller="myController">
<div class="container">
<h1>
GeeksforGeeks
</h1>
<h3>
Approach 2: Using AngularJS Services
</h3>
<p>Edit the message:</p>
<input class="input-field"
type="text"
ng-model="message"
ng-change="updateServiceMessage()" />
<p>
The message is from the service:
{{ serviceMessage }}
</p>
</div>
<script>
var app = angular.module('myApp', []);
app.service('messageService', function () {
var message = "Hello, World!";
this.getMessage = function () {
return message;
};
this.setMessage = function (newMessage) {
message = newMessage;
};
});
app.controller('myController',
function ($scope, messageService) {
$scope.serviceMessage =
messageService.getMessage();
$scope.updateServiceMessage = function () {
messageService.setMessage($scope.message);
$scope.serviceMessage =
messageService.getMessage();
};
});
</script>
</body>
</html>
Output
Similar Reads
How to call an AngularJS Function inside HTML ? A Function is a set of statements that takes input, does some specific computation, and produces output. In this article, we will learn How to Call an AngularJS function inside HTML. To achieve this, we can use {{...}} to call the function from HTML. We can also pass arguments and return the result
3 min read
How to access the value of a Promise in AngularJS ? AngularJS is one of the JS frameworks that consists of promises that are used to handle asynchronous tasks. In some scenarios, we need to handle the promise values by accessing them in the application. So, this can be done using the .then() method and also by using the callbacks in AngularJS. In thi
4 min read
How to allow access outside localhost in AngularJS ? In this article, we will see how to allow access outside the localhost in AngularJS, along with knowing the basic implementation through the example. Firstly, we will discuss how we can build an Angular application, run it, and preview it in the browser. We can easily do this using the CLI, provided
4 min read
How to determine active route in AngularJS ? In this article, we will see how to determine the active route in AngularJS, along with understanding the basic implementation through the example. Approach: To determine which is the active route at any moment in AngularJS this can be achieved by using $on() & $location.path() method. An event
2 min read
How to remove an item from an array in AngularJS Scope? In the AngularJS framework, we can manipulate the data that is within the scope, for example, we can perform the operations on an array by removing its elements. This helps us for better efficiency in developing the web application.We can remove the array elements in the applications like to-do list
5 min read
How to include a JavaScript File in Angular and call a Function from that Script ? In this article, we will learn how to include a JavaScript file in Angular and call a function, defined inside that file, from that script.In Angular, we can include JavaScript script files to import some features or utilize some external libraries or pieces of code, which can be a great asset while
3 min read
How to access index of the parent ng-repeat from child ng-repeat in AngularJS ? In this article, we will see How to access the index of the parent ng-repeat from child ng-repeat in AngularJS.In AngularJS applications, when we implement the nested ng-repeat directives and if we need to access the index of the parent ng-repeat from the child ng-repeat, then we can do this by usin
5 min read
How to use a Custom Service Inside a Filter in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. An Angular service is a broad category that consists of an
4 min read
How to access cookies in AngularJS? AngularJS is the most useful framework for developing dynamic web applications. While developing the applications, we need to store and access the cookies for many other functionalities like retiring the user details, etc. Cookies are nothing but the data that is usually stored in the client's brows
5 min read
How to pass input to a custom directive in Angular JS? In AngularJS, passing input to a custom directive allows the customization of components within an application. Directives can receive input via attributes, expressions, or controller functions, allowing for dynamic behavior and data binding. In this article, we will explore two different approaches
3 min read