Understanding $location Service In AngularJS

Introduction

The $location is very useful service in AngularJS to read or change the URL in the browser. It use "window.location" in base and make URL available to our application. Whatever the changes are made in the URL in the address bar are reflected into $ location service and Changes made by $location are reflected into the browser address bar.

The $location service exposes the current URL in the address bar, so that we can watch, observe and change the URL. It also synchronizes the URL with browser when user click on back or forward button, change URL address bar manually or click any link on page.

Following set of methods supported by $location service

url

It is read/ writes method. It returns url of address bar when called without parameter. It takes one parameter. When we pass the parameter AngularJs try to change the address of browser.

Example:

  1.   var currentURL = $location.url();
Output
If current path in address bar is "http://abc.com/#test/location/url", this method will return "test/location/url".

absUrl

This is read only method. This returns the full url path with all segments.

Example:

HTML: 

  1. <h4>$location Service Example</h4>  
  2. <div ng-controller="HelloController">  
  3.     <div>  
  4.         Current absolute URL: {{currentURL}}  
  5.     </div>  
  6.     <br />  
  7. </div>  
Controller:
  1. var app = angular.module("app", []);  
  2. app.controller("HelloController", function ($scope, $location) {  
  3.     $scope.currentURL = $location.absUrl();  
  4. });  

Output:

If current path in address bar is "http://localhost:42382/TestAgularJs.html ", this method will return full path url i.e.”http://localhost:42382/TestAgularJs.html".

 
 

port

This is read only method. It returns the port Id of current url.

Example:

HTML:

  1. <h4>$location Service Example</h4>  
  2. <div ng-controller="HelloController">  
  3.     <div>  
  4.         Current port used by the application URL: {{currentPort}}  
  5.     </div>  
  6.     <br />  
  7. </div>  
Controller:
  1. var app = angular.module("app", []);  
  2. app.controller("HelloController", function ($scope, $location) {  
  3.     $scope.currentPort = $location.port();  
  4. });  
 Output:
 
 

protocol

This is read only method. It returns protocol of current url.

Example:

HTML:

  1. <h4>$location Service Example</h4>  
  2. <div ng-controller="HelloController">  
  3.     <div>  
  4.         Current Protocol: {{currentProtocol}}  
  5.     </div>  
  6.     <br />  
  7. </div>  
Controller:
  1. app.controller("HelloController", function ($scope, $location) {  
  2.     $scope.currentProtocol = $location.protocol();  
  3. });  

Output:

 
 

host

This is read only method. It returns host of current url. It only return host name whereas non-angular version location.host returns host name along with post id.

Example:

HTML: 

  1. <h4>$location Service Example</h4>  
  2. <div ng-controller="HelloController">  
  3.     <div>  
  4.         Current Host: {{currentHost}}  
  5.         <br />  
  6.         Current Host (using non-Angular method): {{nonAngularHost}}  
  7.     </div>  
  8.     <br />  
  9. </div>  
Controller:
  1. var app = angular.module("app", []);  
  2. app.controller("HelloController", function ($scope, $location) {  
  3.     $scope.currentHost = $location.host();  
  4.     $scope.nonAngularHost = location.host;  
  5. });  
Output: 
 
 

path

This is read / writes method. It takes optional parameter. It returns current url path when called without parameter and when pass url as a parameter, it change path. It should always start with forward slash (/).

Example: 
  1. var currentPath = $location.path();  

Output:

If current path in address bar is "http://abc.com/#test/location/path?test=new", this method will return "test/location/path".

search

This is read / writes method. It returns search part of current url as obect when called without parameter. When call with parameter, it change search part with parameter and return $location object.

Example:

HTML: 

  1. <h4>$location Service Example</h4>  
  2. <div ng-controller="HelloController">  
  3.     <div>  
  4.         Current search: {{currentSearch}}  
  5.         <br />  
  6.         <br />  
  7.         <button ng-click="changeValue()">change value</button>  
  8.         <br />  
  9.         <br />  
  10.         New Value : {{newSearchValue}}  
  11.     </div>  
  12.     <br />  
  13. </div>  
Controller:  
  1. var app = angular.module("app", []);  
  2. app.controller("HelloController", function ($scope, $location) {  
  3.     $scope.currentSearch = $location.search();  
  4.     $scope.changeValue = function () {  
  5.         $location.search("name""tejas");  
  6.         $scope.newSearchValue = $location.search();  
  7.     }  
  8. });  
 
Output:
 
 

hash

This is read / writes method. It returns hash part of current url when called without parameter. When call with parameter, it change hash part with parameter and return $location object.

Example:

HTML:  
  1. <h4>$location Service Example</h4>  
  2. <div ng-controller="HelloController">  
  3.     <div>  
  4.         Current hash value: {{currentHash}}  
  5.         <br />  
  6.         <br />  
  7.         <button ng-click="changeHashValue()">change value</button>  
  8.         <br />  
  9.         <br />  
  10.         New Value : {{newHashValue}}  
  11.     </div>  
  12. </div>  
Controller:
  1. var app = angular.module("app", []);  
  2. app.controller("HelloController", function ($scope, $location) {  
  3.     $scope.currentHash = $location.hash();  
  4.     $scope.changeHashValue = function () {  
  5.         $location.hash("NewHash");  
  6.         $scope.newHashValue = $location.hash();  
  7.     }  
  8. });  
Output: 
 
 

state

This is read / writes method. It returns history state when called without parameter. When call with parameter, it change history state with parameter and return $location object. This method is using internal HTML5 History API, so it is supported only in HTML5 mode.

replace

It replace all changes to the $location with the current history record. It is not adding new value in history. Following set of Events supported by $location service

$locationChangeStart

This event is broadcasted before a URL will change. The new state and old state parameter defined in HTML 5 mode only. This event has following parameters

  • angularEvent: It is Synthetic event object.
  • newUrl: New URL
  • oldUrl: This is optional parameter. This contains old URL.
  • newState: This is optional parameter. This contains new history state object
  • oldState: This is optional parameter. This contains old History state object which was before it was changed.
 $locationChangeSuccess

This event is broadcasted after a URL was changed. This event has same parameter or argument as $locationChangeStart event.

Summary

This article will help you to understand location service in AngularJS.

References

https://docs.angularjs.org/api/ng/service/$location

 


Similar Articles