Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload child component when variables on parent component changes. Angular2

Tags:

angular

I have a MasterComponent which loads header, footer, sidebar etc. On the header there is a dropdown whose options are set once the user is logged in. I want the header to be constant even if I navigate to different routes which loads different child component. Means that the selected option should not change and value of dropdown should be accessible in all the child component. Onchange of dropdown value the current child component should be updated/reloaded.

How should I approach this problem? I want to event-listener kind of functionality. Once the model from MasterComponent Changes , reload the current child component. On MasterComponent's variable variable update ChildComponent will listen to the update and run some function or call some API again and reload the ChildComponent.

// routes const appRoutes: Routes = [     {         path: '',         redirectTo: 'login',         pathMatch: 'full',     },     {   path: 'login', component: LoginComponent },     {   path: 'logout', component: LogoutComponent },     {         path: '',         component: MasterComponent,         canActivate: [AuthGuard],         children: [             { path: 'record/create', component: RecordCreateComponent }, // create record for selectedRestaurant in MasterComponent             { path: 'record/', component: RecordComponent }, // shows all record of current selectedRestaurant in MasterComponent             { path: 'record/:id/update', component:RecordUpdateComponent }, // show form to edit record having id             { path: 'record/:id', component: RecordComponent }, // show record details having id         ]     },     { path: '**', redirectTo: 'login' } ]; 
// MasterComponent @Component({     selector: 'master',     templateUrl: templateUrl,     styleUrls:[styleUrl1]  }) export class MasterComponent implements AfterViewInit, OnInit{     restaurants: Array<Restaurant> = [];     user:User;     selectedRestaurant: Restaurant;      constructor(private router: Router, private storageHelper:StorageHelper){     }     ngAfterViewInit() {     }     ngOnInit(){         this.user = JSON.parse(this.storageHelper.getItem('user'));         this.restaurants = this.user.restaurants;         this.selectedRestaurant = this.restaurants[0];         this.router.navigate(['/record/' + this.selectedRestaurant.id]);     }     onRestaurantChange(){         this.router.navigate(['/record/' + this.selectedRestaurant.id]);     }     createRecord(){     } } 

enter image description here

like image 569
Kaushal Kumar Avatar asked Oct 26 '16 08:10

Kaushal Kumar


People also ask

How do you refresh a component from another component in Angular 9?

To refresh, or better to say update another component from a different component, we can use the concept of Observables and Subject (which is a kind of Observable). This concept has an added benefit when data are to be received from APIs for CRUD operations.


2 Answers

Use @Input to pass your data to child components and then use ngOnChanges (https://angular.io/api/core/OnChanges) to see if that @Input changed on the fly.

like image 124
Vladimir Tolstikov Avatar answered Sep 23 '22 02:09

Vladimir Tolstikov


update of @Vladimir Tolstikov's answer

Create a Child Component that use ngOnChanges.

ChildComponent.ts::

import { Component, OnChanges, Input } from '@angular/core'; import { ActivatedRoute } from '@angular/router';  @Component({   selector: 'child',   templateUrl: 'child.component.html', })  export class ChildComponent implements OnChanges {   @Input() child_id;    constructor(private route: ActivatedRoute) { }    ngOnChanges() {     // create header using child_id     console.log(this.child_id);   } } 

now use it in MasterComponent's template and pass data to ChildComponent like:

<child [child_id]="child_id"></child> 
like image 22
suhailvs Avatar answered Sep 20 '22 02:09

suhailvs