How to get the current URL using AngularJS ?
In this article, we are going to see how to get the current URL with the help of AngularJS. We will be using the $location.absURL() method to get the complete URL of the current page.
Syntax:
$location.absURL()
Example 1: Just use the $location.absURL() method to get the complete URL of the current page.
HTML
<!DOCTYPE HTML><html> <head> <script src= </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function ($scope, $location) { $scope.url = ''; $scope.getUrl = function () { $scope.url = $location.absUrl(); }; }); </script></head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> Get current URL in angularJS </p> <div ng-app="app"> <div ng-controller="controller"> <p>Url = {{url}}</p> <input type="button" value="Click to get URL" ng-click="getUrl()"> </div> </div></body> </html> |
Output:

Example 2: Similar to the previous example but using the split() method to get the domain name of the URL.
HTML
<!DOCTYPE HTML><html> <head> <script src= </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function ($scope, $location) { $scope.url = ''; $scope.getUrl = function () { // Will change the current url to // ide.geeksforgeeks.org $scope.url = $location .absUrl().split('/')[2];; }; }); </script></head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> Get current URL in angularJS </p> <div ng-app="app"> <div ng-controller="controller"> <p>Url = {{url}}</p> <input type="button" value="Click to get URL" ng-click="getUrl()"> </div> </div></body> </html> |
Output:



