Search code examples
javascriptangularroutesangular2-routing

Change route params without reloading in Angular 2


I'm making a real estate website using Angular 2, Google Maps, etc. and when a user changes the center of the map I perform a search to the API indicating the current position of the map as well as the radius. The thing is, I want to reflect those values in the url without reloading the entire page. Is that possible? I've found some solutions using AngularJS 1.x but nothing about Angular 2.


Solution

  • You could use location.go(url) which will basically change your url, without change in route of application.

    NOTE this could cause other effect like redirect to child route from the current route.

    Related question which describes location.go will not intimate to Router to happen changes.

    How to

    import { Location } from '@angular/common';
    
    constructor(private _location: Location){ }
    
    updateUrl(url: string) {
      this._location.go(url)
    }