John
John

Reputation: 307

Angular refreshing page with a button

What I want to do is have a button that does the same thing as the refresh button in chrome does. I have tried location.reload(), and it doesn't seem to really do anything. Is there any way to do this?

exit() {
    location.reload();
  }

 <button [routerLink]="['/dashboard']" (click)="exit()">Exit</button>

Upvotes: 7

Views: 53003

Answers (4)

rohithpoya
rohithpoya

Reputation: 1005

You can remove the [routerLink]="['/dashboard']" from the button.

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

 <button (click)="exit()">Exit</button>

Upvotes: 0

John Adeniran
John Adeniran

Reputation: 124

What about using

HTML:

<button (click)="reload()">click me</button>

TS:

reload(){
  // any other execution
  this.ngOnInit()
}

Upvotes: -1

Crystal
Crystal

Reputation: 1505

This will reload to default route page

import { DOCUMENT } from '@angular/common';
import { Component, Inject } from '@angular/core';

@Component({
  selector: 'app-refresh-banner-notification',
  templateUrl: './refresh-banner-notification.component.html',
  styleUrls: ['./refresh-banner-notification.component.scss']
})
export class RefreshBannerNotificationComponent {

  constructor(
    @Inject(DOCUMENT) private _document: Document
  ) {}

  refreshPage() {
    this._document.defaultView.location.reload();
  }
}

Upvotes: 2

CodeWarrior
CodeWarrior

Reputation: 2851

Try this: window.location.reload();

Upvotes: 19

Related Questions