The Angular Location service is used to interact with the browser URL. We can use it to track the URL changes. Use it to read the current URL, modify the URL, go back or forward through the browser history, etc. In this article, we will look at how to make use of Angular Location service. We learn how to make use of location.back()
, location.forward()
, location.path()
, location.go()
, location.replacestate()
, location.getState()
, location.onUrlChange()
etc
We usually make use of Angular Router to navigate to different parts of the application. The location service does not perform navigation but allows us to manipulate the URL. It bypasses the Angular Router completely.
Table of Contents
How to use Location Service
The Location service is part of the @angular/common
package. Hence import it in the Component, where you want to use it.
1 2 3 | import { Location } from '@angular/common'; |
And finally, inject it to the component, where you want to use it
1 2 3 4 5 6 | export class MyComponent { constructor(private location: Location) { } } |
Going back and forward
back()
Use location.back()
to go back to the previous URL.
1 2 3 4 5 | goBack() { this.location.back(); } |
forward()
Use location.forward()
to go to the next URL.
1 2 3 4 5 | goForward() { this.location.forward(); } |
Get the current path
path()
The current URL path can be obtained using the location.path()
1 2 3 4 5 | getPath() { return this.location.path(); } |
Manipulate the URL
go()
Use the location.go()
to change the current URL to a new given URL. It also pushes the new URL into the browser history. The location.go()
does not navigate to the new URL. To navigate to the new URL, you must use the Angular router
1 2 3 4 | Syntax: go(path: string, query: string = '', state: any = null): void |
1 2 3 | location.go("/product/1"); |
Note that, you can also change the state object of the URL.
replaceState()
Use the location.replaceState()
to change the current URL to a new given URL. It replaces the top item in the browser history.
This is very similar to location.go()
with one difference. The location.go()
add the URL to the browser history, while location.replaceState()
replaces the top item in the history with the newly added one
1 2 3 4 | Syntax replaceState(path: string, query: string = '', state: any = null): void |
1 2 3 4 | Examples: location.replaceState(): |
Get History State Object
getState()
1 2 3 | location.getState(): |
The above introduced in Angular 8, returns the current value of history. state object. You can use this method to pass the dynamic data via the Angular Routes
You can pass the state object by using the following ways.
- By using
routerLink
directive. - By using the
router.navigateByUrl()
method. - via
location.go()
method of the location service - via
location.replaceState()
method of the location service
1 2 3 4 5 6 7 8 | <a [routerLink]="['dynamic']" [state]="{ id:1 , name:'Angular'}">Dynamic Data</a> this.router.navigateByUrl('/dynamic', { state: { id:1 , name:'Angular' } }); location.go("/product/1","", { id:1 , name:'Angular'}) location.replaceState("/product/1","", { id:1 , name:'Angular'}) |
Listen to URL Changes
onUrlChange()
Use onUrlChange to listen for a change in URLs. This API can be used to catch updates performed by the Angular framework. These are not detectable through “popstate” or “hashchange” events.
1 2 3 | onUrlChange(fn: (url: string, state: unknown) => void) |
1 2 3 4 5 6 7 8 9 | ngOnInit() { this.location.onUrlChange( (url: string, state: unknown) => { console.log("Location changes to "+url); console.log(state); }) } |
Subscribe to the platform’s popState
events.
Subscribe to the location service to listen to the platform’s popState events
The popState events are fired when
- User clicks on the Back/Forward button on the browser window
- location.forward() & location.back() is called.
And not fired when
Location.go()
andlocation.replaceState()
is used.
1 2 3 | subscribe(onNext: (value: PopStateEvent) => void, onThrow?: (exception: any) => void, onReturn?: () => void): SubscriptionLike |
1 2 3 4 5 6 7 8 9 10 11 12 | this.location.subscribe( ( (value:PopStateEvent) => { console.log("locaton OnNext") console.log(value); }), ( ex => { console.log("Error occured postate event") console.log(ex); }) ) |
Summary
It’s better to use the Router service to trigger route changes. Use Location Services, only when you need to manipulate the Router, without resulting in a page refresh.
test
this.location.back() back is not working.
import Location service into component and inject it in constructor then use it. it will work
good
Not sufficient.