how to refresh page in angular 2 – Angular

Photo of author
Written By M Ibrahim
angular angular-ui-router

Quick Fix: To refresh your Angular 2 page, simply use the ‘location.reload()’ method. For example, in your component, you can add the following code: this.location.reload();. This will cause the page to reload.

The Problem:

In Angular2 application, a router link loads a component (ProductsStartComponent) that dynamically renders other components using ngIf. When navigating back to the ProductsStartComponent using the router link, the page does not refresh, causing the previously rendered components to remain visible. How can I refresh or reload the page using Angular 2’s routing mechanism to ensure that the ProductsStartComponent is reset and its initial state is displayed?

The Solutions:

Solution 1: Navigate Back to Specific Component

To reload the page when a route link is clicked, you can use the following steps:

  1. Import the Location service from `@angular/common`.
  2. In the component where you want to reload the page, inject the `Location` service into the constructor.
  3. When the route link is clicked, call the `location.reload()` method to reload the page.

Here is an example of how to reload the page when a route link is clicked:
“`
import { Component, OnInit } from ‘@angular/core’;
import { Location } from ‘@angular/common’;

@Component({
selector: ‘app-reload-page’,
templateUrl: ‘./reload-page.component.html’,
styleUrls: [‘./reload-page.component.css’]
})
export class ReloadPageComponent implements OnInit {

constructor(private location: Location)

ngOnInit(): void

reloadPage() {
this.location.reload();
}
}

In the HTML template, you can add the following code to a button to reload the page:

<button (click)="reloadPage()">Reload Page

When the button is clicked, the `reloadPage()` method will be called and the page will be reloaded.

Solution 2: Call window.location.reload()

To refresh the page in Angular 2, you can call the window.location.reload() function. However, you cannot call this function directly from an expression. If you want to call it from a click event, you need to put it in a function.

For example, you can add the following code to your HTML template:

<button (click)="reloadPage()">Reload Page</button>

And then define the reloadPage() function in your component class:

reloadPage() {
  window.location.reload();
}

If you want to add a slight delay before reloading the page, you can use the following code:

reloadPage() {
  setTimeout(() => {
    window.location.reload();
  }, 100);
}

This will ensure that the click event has been processed before the page is reloaded.

Solution 3: Implement page refresh

Within your component:

location.reload();

Solution 5: Reloading the Current Route

To refresh or reload the page in Angular 2 when clicking on a router link that loads components via ngIf instead of navigation, you can use the following approach:

  1. In your component, create a method called reloadCurrentRoute().

  2. In the reloadCurrentRoute() method, use the router.navigateByUrl() method to navigate to the current URL with the {skipLocationChange: true} option. This will cause the router to reload the current route without changing the URL in the browser’s address bar.

  3. After navigating away from the current URL, use the router.navigate() method to navigate back to the current URL. This will trigger a navigation event and reload the components.

  4. Call the reloadCurrentRoute() method when you want to refresh or reload the page, such as after saving data or performing an operation.

Here’s an example of how you can use the reloadCurrentRoute() method in your Angular 2 component:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'my-component',
  templateUrl: './my-component.html',
  styleUrls: ['./my-component.css'],
})
export class MyComponent implements OnInit {

  constructor(private router: Router) {}

  ngOnInit(): void { }

  reloadCurrentRoute() {
    let currentUrl = this.router.url;
    this.router.navigateByUrl('/', { skipLocationChange: true }).then(() => {
      this.router.navigate([currentUrl]);
    });
  }

}

By implementing this approach, you can refresh or reload the page in Angular 2 when navigating to a router link that loads components via ngIf instead of navigation.