113

How can I reload the current page on Angular 2?

if iam in page 2 (pagination) and refresh the page it will show page 1(URL pageload) but i want i refresh the page 2 and it will appear in page 2

11
  • What do you want to achieve by reloading? May be there are alternate ways to achieve the same? May 15, 2017 at 17:59
  • @MadhuRanjan i want to reset all the model but not value after value,
    – FABI1987
    May 15, 2017 at 18:01
  • you may have all model fetch functions in a common method and call it when ever needed. May 15, 2017 at 18:10
  • 2
    usually Browser features work irrespective of platform, if it is available officially for the respective OS. Having said that I would suggest you to create a new SO question regarding how to get around window.location.reload() for chrome on Linux, that would be ideal place for getting your answer, and then you may have a check in your code to get the browser version and have a logic around, Cheers!! May 15, 2017 at 19:24
  • 1
    FWIW, my own use case for this is multi-language support, since as of v5.1, Angular still doesn't support it without doing a separate deployment per language. I have a Razor page that decides which Angular script bundle to load, depending on the language preference it gets from the user database of the ASP.NET Core backend. In order to change the language after the user has changed their preferences, reloading just the Angular SPA won't do, I need to reload the entire Razor page to basically have it load a different Angular SPA. Dec 18, 2017 at 12:56

1 Answer 1

249

This should technically be achievable using window.location.reload():

HTML:

<button (click)="refresh()">Refresh</button>

TS:

refresh(): void {
    window.location.reload();
}

Update:

Here is a basic StackBlitz example showing the refresh in action. Notice the URL on "/hello" path is retained when window.location.reload() is executed.

4
  • 8
    i want better way windows.location that not cross platform...
    – FABI1987
    May 15, 2017 at 18:20
  • There is no simple cross platform way. You would have to check what platform it is and then do the appropriate reload for that platform.
    – Joo Beck
    Jan 16, 2018 at 16:25
  • The refresh doesn't work and only brings the user back to the first page.
    – Ace
    Jan 21, 2018 at 20:00
  • It can depend on how your routing is set up as well as how SPA routes are loaded by the server. reload() is a standard browser approach to reloading resources from the current URL. Jan 21, 2018 at 20:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.