Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to refresh page in angular 2

I have created one router link as below. This router link loads ProductsStartComponent and then this component loads several other components using ngif and not via navigation. Since below Product categories link is visible in all pages so if I am clicking on this link after reaching to some component of ngif , this is not taking me back on ProductsStartComponent.

As I am new to Angular , my understanding for this behavior is because all values/models are set and that's why it's not navigating. I suppose this can be achieved by refresh or reload of page but how to achieve that. Please advise.

In admin.component.html, router Link is defined for clicking.
returns

<li routerLinkActive="active"><a routerLink="categories"><p>Products Categories</p></a></li> 

returns

In app-routing.module.ts, which component need to be loaded on clicking of router link

const appRoutes: Routes = [ {path: 'admin', component: AdminComponent, children: [     { path: 'dashboard', component: AdminDashboardComponent },     { path: 'sellers', component: AdminSellersComponent },     { path: 'categories', component: ProductsStartComponent} ]}] 

In product-start.component.html, this is being loaded on first click on router link. Now if I click on edit button and moved to other component and again if i click on product categories router link , nothing happen , I am expecting it to reset the page.

<div>     <div *ngIf="!isChildProductClicked; else notClicked" >        <app-admin-products (productId)="received($event)"></app-admin-products>     </div>     <ng-template #notClicked><app-child-products [selProdIndex]=productIndex></app-child-products></ng-template> </div> 
like image 805
user2800089 Avatar asked Aug 10 '17 07:08

user2800089


People also ask

How can I refresh a page in angular?

One is to use window. reload to reload the entire page, and the other is to use the onSameUrlNavigation-reload refresh component with an angle router.

What happens when we refresh a page in angular?

From then on, the Angular framework kick in and loads the appropriate modules and pages as route changes. When you refresh the page you lose all the state of the application since you are reloading the index. html with all the required dependencies again.


2 Answers

If you want to reload the page , you can easily go to your component then do :

location.reload(); 
like image 133
ZAhmed Avatar answered Sep 18 '22 06:09

ZAhmed


Just in case someone else encounters this problem. You need to call

window.location.reload() 

And you cannot call this from a expression. If you want to call this from a click event you need to put this on a function:

(click)="realodPage()" 

And simply define the function:

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

If you are changing the route, it might not work because the click event seems to happen before the route changes. A very dirty solution is just to add a small delay

reloadPage() {     setTimeout(()=>{       window.location.reload();     }, 100); } 
like image 27
Blasco Avatar answered Sep 18 '22 06:09

Blasco