Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload a page in angular2

How to reload a page in angular2.
While searching through net, i got a code "this._router.renavigate()" to reload the page but looks like its not working with latest release of angular2.
Another way is 'window.location.reload()' but this is not the angular way of doing it.

like image 394
Krishna Avatar asked Jun 16 '16 07:06

Krishna


1 Answers

If you really want to reload the page you need to run location.reload() outside the angular zone

this.zone.runOutsideAngular(() => {
    location.reload();
});

tested with RC6

Full Example

import { Component, NgZone } from "@angular/core";

@Component({        
    templateUrl: "template.html"    
})
export class ReloadComponent{
    constructor(
        private zone: NgZone) {
    }

    reloadPage() { // click handler or similar
        this.zone.runOutsideAngular(() => {
            location.reload();
        });
    }
}
like image 111
kodebot Avatar answered Oct 11 '22 05:10

kodebot