Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies.
I love to learn new things in life that keep me motivated.
$location Provides methods and properties for browser location manipulation. The $location service is passed to the controller as an argument. To use a service in a controller, it must be defined as a dependency.
You can use the window.location property to find the location of the webpage but it has some limitations in Angular applications, so using the
$location service is the best option for Angular applications.
Example-
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>ng-customFilter 2</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.8.2/angular.min.js"></script>
</head>
<body data-ng-controller="myController">
<div class="container my-4">
<div class="row">
<!-- show location of current webpage -->
<div class="col-12">
<p>Using $location - <span class="fw-semibold">{{myLocation}}</span></p>
<!-- using the window property -->
<p>Using Window.location - <span class="fw-semibold">{{winLocation}}</span></p>
</div>
</div>
</div>
<script>
angular.module("myApp", [])
.controller("myController", function($scope, $location){
// return the location of webpage
$scope.myLocation = $location.absUrl();
// alternate way to find the webpage location
$scope.winLocation = window.location;
});
</script>
</body>
</html>
Output-
Using $location - http://127.0.0.1:5500/TestFile.html
Using Window.location - http://127.0.0.1:5500/TestFile.html
I hope you have understood how to use $location service in AngularJS application.
Thank you!
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
$location Provides methods and properties for browser location manipulation. The $location service is passed to the controller as an argument. To use a service in a controller, it must be defined as a dependency.
You can use the window.location property to find the location of the webpage but it has some limitations in Angular applications, so using the $location service is the best option for Angular applications.
Example-
Output-
I hope you have understood how to use $location service in AngularJS application.
Thank you!