1

I am working on angular application where I want to update router url on some condition. below is the sample code:-

console.log(this.router.url) // it prints '/users'
this.location.replaceState('/users/1') // it updates the url bar with new url
console.log(this.router.url)  //it prints same again '/user'

is there any way i can change the current route url without refreshing or reloading the page.

Sachin
  • 166
  • 1
  • 8
  • If I understood correctly, you want to navigate without refreshing the page? Try: `this.router.navigate(['/user/1']);` and check out the official [documentation](https://angular.io/guide/router). – riorudo Feb 03 '22 at 08:13

2 Answers2

0

you probably should navigate to new route

if (condition) {
  this.router.navigate('/users/1');
}

In Angular (as SPA) it won't refresh or reload page but instantiate component for new route. If you want display user information in only one div in the page without refreshing the rest (i.e. list of users), you can make use of router-outlet.

You can learn more here

Walter Luszczyk
  • 1,369
  • 2
  • 19
  • 36
0

you should add a condition in the ngOnInit if (this.router.url === '/cms/page/edit') { this.page1= true

and then add a *ngIf="page1" or *ngIf="!page1" to the html balise.

this worked for me ;).

Farouk Mhamdi
  • 311
  • 2
  • 8