Angular 16 Location Service Tutorial with Example

Last Updated on by in Angular
Angular location service will be dealt with in this article, location service is a part of Angular. Thanks to this service, you can easily interact with the path of the current URL.When it comes to navigation needs, we mostly depend on the Angular router.

However, in some cases, you need to rely on the location service.

Here you will affect the URL without having to involve the router. Also, at times, you can combine the router and the location service to perform a few operations.

In order to remove strict type warnings or errors make sure to set “strict”: false and "strictTemplates": false under compilerOptions and angularCompilerOptions properties in tsconfig.json file.

Angular Location Service Example

To gain access to the Angular $location service, you import it along with LocationStrategy & PathLocationStrategy from @angular/common. You can inject Location inside the constructor.

This service allows Angular app to communicate with the browser’s URL.

LocationStrategy service, gets the route state of an Angular app from the browser’s URL. Angular offers 2 location strategies: – HashLocationStrategy and PathLocationStrategy.

This service configures the Location service by following the hash fragment of the browser’s URL.

This service configures the Location service by following its state in the browser’s URL.

Following is the way of importing location services in Angular:

// ...
import {Location, LocationStrategy, PathLocationStrategy} from '@angular/common';
  // ...
  providers: [Location, {provide: LocationStrategy, useClass: PathLocationStrategy}]

export class AppComponent {
  location: Location;
  constructor(location: Location) { this.location = location; }

Going Forward and Backward in Angular

In case if you want the method to go to and fro in the navigation:

GoForward() {

GoBack() {

Getting Current Path

In order to get the current path, you may use the location.path method:

getCurrentPath() {

Angular Location Service Methods

go: URL will be changed and will append it to the history of the browser.

replaceState: Here it changes the URL. It will also the URL that appears on top in history. This way, when you try to go back, you always go back to the one before.

isCurrentPathEqualTo: It simply compares 2 given path values.

normalize: While taking a path, it will be returning a normalized path.

Get Current Path with Event Observable

The Angular router service offers events observable, If you wish to detect the navigation changes, then subscribe to the events method which will be returning an observable.

Let’s assume that we wish to detect the changes in the URL.

We have used a baseURL member variable for the same:

import { Component } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [Location],

export class AppComponent {
  baseURL: any;
  constructor(private location: Location, private router: Router) {}

  ngOnInit() { => {
      if (this.location.path() !== '') {
        this.baseURL = false;
      } else {
        this.baseURL = true;


Finally, we have completed Angular Location service tutorial with examples.