We will discuss View Child and View Children decorators in angular with practical implementation with the help of angular.

Overview

Implementation

Child component HTML File

<div class="alert alert-secondary" role="alert" style="padding: 30px;margin: 40px;">
    <h3 #childheading>Child Component</h3>
</div>

Child Component TypeScript file

import {
    Component,
    ElementRef,
    Renderer2,
    ViewChild
} from '@angular/core';
@Component({
    selector: 'app-child',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.css']
})
export class ChildComponent {
    //View Child
    @ViewChild("childheading") childheading!: ElementRef;
    constructor(private renderer: Renderer2) {}
    ngOnInit(): void {}
    ngAfterViewInit(): void {
        console.log(this.childheading)
        //ViewChild
        this.renderer.setStyle(this.childheading.nativeElement, 'background-color', 'red');
    }
}

View Child and View Children in Angular

Suppose we want to access another heading with the same reference variable as a child heading; that is not possible using View Child. We need to use View Children because View Child only points to the first matching DOM reference element.

Let’s start the implementation of View Children,

Child Component HTML File

<div class="alert alert-secondary" role="alert" style="padding: 30px;margin: 40px;">
    <h3 #childheading>Child Component</h3>
    <h3 #childheading> Demo </h3>
</div>

Child Component TypeScript file

import {
    Component,
    QueryList,
    Renderer2,
    ViewChildren
} from '@angular/core';
@Component({
    selector: 'app-child',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.css']
})
export class ChildComponent {
    //View Children
    @ViewChildren("childheading") childheading!: QueryList < any > ;
    constructor(private renderer: Renderer2) {}
    ngOnInit(): void {}
    ngAfterViewInit(): void {
        console.log(this.childheading)
        //ViewChildren
        this.renderer.setStyle(this.childheading.first.nativeElement, 'color', 'red');
        this.renderer.setStyle(this.childheading.last.nativeElement, 'color', 'blue');
    }
}

View Child and View Children in Angular

Here you can see the query list with two native element details first and last.

This is about accessing the DOM elements using ViewChild and View Children.

Now we will access the child component method into the parent component by using the View Child decorator.

Child Component HTML file

<div class="alert alert-secondary" role="alert" style="padding: 30px;margin: 40px;">
    <h3>Child Component</h3>
    <h5>{{message}}</h5>
</div>

As you can see, we use string interpolation to display the message when the method is executed.

Child Component TypeScript file

import {
    Component
} from '@angular/core';
@Component({
    selector: 'app-child',
    templateUrl: './child.component.html',
    styleUrls: ['./child.component.css']
})
export class ChildComponent {
    message: string = "";
    constructor() {}
    ngOnInit(): void {}
    //Child Method
    childMethod() {
        this.message = 'Hi, Parent Component Invoke Child Method...';
    }
}

Inside the child component, declare one message property and one child method that just put some string message inside our property, and now we will execute this child method from the parent component.

Parent Component TypeScript File

import {
    Component,
    ViewChild
} from '@angular/core';
import {
    ChildComponent
} from './child/child.component';
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    title = 'viewchildandchildrendemo';
    @ViewChild("childMethod") method!: ChildComponent
    parentFunction() {
        this.method.childMethod();
    }
}

Parent Component HTML File

<nav class="navbar navbar-light bg-light" style="padding: 30px;margin: 40px; justify-content:center">
  <span class="navbar-brand mb-0 h1">Angular Component Communication</span>
</nav>

<div class="alert alert-primary" role="alert" style="padding: 30px;margin: 40px;">
  <h3>Parent Component</h3>
  <button (click) = "parentFunction()">Call child component method</button>
</div>

<app-child #childMethod></app-child>

View Child and View Children in Angular

So, this is all about View Child and View Children.

GitHub Link

https://github.com/Jaydeep-007/ViewChildAndChildrenDemo

Happy Coding!