Skip to content
the poor coder

reload a child component in angular

Poor Man
Published
Reading time
3 min read
On this page

Reloading a Child Component in Angular#

If you are working with Angular, you may have found yourself in a situation where you need to reload a child component. This can be done in a few different ways, depending on your specific use case.

Using ViewChild#

One way to reload a child component is by using the ViewChild decorator in your parent component. This allows you to access the child component's methods and properties directly from the parent.

TypeScript

// parent.component.ts
import { Component, ViewChild } from '@angular/core';
import { ChildComponent } from './child.component';

@Component({
  selector: 'parent-component',
  template: `
    <child-component #child></child-component>
    <button (click)="reloadChild()">Reload Child</button>
  `
})
export class ParentComponent {
  @ViewChild('child') childComponent: ChildComponent;

  reloadChild() {
    this.childComponent.ngOnInit();
  }
}

In this example, we are using the ViewChild decorator to get a reference to the ChildComponent, and then calling its ngOnInit method when the "Reload Child" button is clicked. This will trigger the child component to be reloaded.

Using Input Properties#

Another way to reload a child component is by using input properties. This method involves passing a new value to the child component's input property, which will trigger the component to be re-rendered with the new value.

TypeScript

// parent.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'parent-component',
  template: `
    <child-component [data]="childData"></child-component>
    <button (click)="reloadChild()">Reload Child</button>
  `
})
export class ParentComponent {
  childData = { name: 'Raju', age: 25 };

  reloadChild() {
    this.childData = { ...this.childData };
  }
}

// child.component.ts
import { Component, Input } from '@angular/core';

@Component({
  selector: 'child-component',
  template: `
    <p>Name: {{ data.name }}</p>
    <p>Age: {{ data.age }}</p>
  `
})
export class ChildComponent {
  @Input() data: any;
}

In this example, we are passing a data object to the child component's input property. When the "Reload Child" button is clicked, we are creating a new object with the same values as the original, and assigning it to the childData property. This will trigger the child component to be re-rendered with the new data.

Using a Service#

A third way to reload a child component is by using a service to communicate between the parent and child components. This method involves creating a shared service that both components can access, and using it to trigger a method on the child component.

TypeScript

// parent.component.ts
import { Component } from '@angular/core';
import { ReloadService } from './reload.service';

@Component({
  selector: 'parent-component',
  template: `
    <child-component></child-component>
    <button (click)="reloadChild()">Reload Child</button>
  `,
  providers: [ReloadService]
})
export class ParentComponent {
  constructor(private reloadService: ReloadService) {}

  reloadChild() {
    this.reloadService.reloadChild();
  }
}

// child.component.ts
import { Component } from '@angular/core';
import { ReloadService } from './reload.service';

@Component({
  selector: 'child-component',
  template: `
    <p>{{ message }}</p>
  `
})
export class ChildComponent {
  message = 'Original Message';

  constructor(private reloadService: ReloadService) {
    this.reloadService.reload$.subscribe(() => this.ngOnInit());
  }

  ngOnInit() {
    this.message = 'New Message';
  }
}

// reload.service.ts
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class ReloadService {
  private reloadSource = new Subject<void>();
  reload$ = this.reloadSource.asObservable();

  reloadChild() {
    this.reloadSource.next();
  }
}

In this example, we are using a shared service called ReloadService to communicate between the parent and child components. The parent component calls the reloadChild method on the service, which triggers the ngOnInit method in the child component. The child component subscribes to the reload$ observable in the service, and will call its ngOnInit method whenever the observable emits a value.

These are three different ways to reload a child component in Angular. Each method has its advantages and disadvantages, so choose the one that best fits your specific use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC