Thuta Learning
Angular
IntermediateWeb DevelopmentIntermediate

ViewChild & ViewChildren

AngularLesson 2020 min

What you'll walk away with

  • Explain the core concept and Angular data flow of ViewChild & ViewChildren
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

ViewChild & ViewChildren, in plain language

@ViewChild is used in a parent component to access a child component or a DOM element directly.

Problem → Why → Solution

The problem — why this concept matters

A parent calling many child methods through ViewChild breaks component encapsulation.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.

Mental model
  1. input and state
  2. component and template
  3. rendered Angular UI

Explain the core concept and Angular data flow of ViewChild & ViewChildren

Core syntax or pattern

typescript
// child.component.ts
export class ChildComponent {
  message = 'Hello from Child!';
  
  showAlert() {
    alert('Child method called!');
  }
}

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

@Component({
  selector: 'app-parent',
  template: `
    <app-child></app-child>
    <button (click)="callChildMethod()">Call Child</button>
  `
})
export class ParentComponent implements AfterViewInit {
  @ViewChild(ChildComponent) child!: ChildComponent;
  
  ngAfterViewInit() {
    console.log(this.child.message);
  }
  
  callChildMethod() {
    this.child.showAlert();
  }
}

View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.

Read the code step by step

  1. input and state// child.component.ts

    @ViewChild is used in a parent component to access a child component or a DOM element directly.

  2. component and template<app-child></app-child>

    View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.

  3. rendered Angular UI}

    Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.

Execution flow
  1. input and state
  2. component and template
  3. rendered Angular UI

Success

Explain the core concept and Angular data flow of ViewChild & ViewChildren

Failure

A parent calling many child methods through ViewChild breaks component encapsulation.

@ViewChild is used in a parent component to access a child component or a DOM element directly.

typescript
// child.component.ts
export class ChildComponent {
  message = 'Hello from Child!';
  
  showAlert() {
    alert('Child method called!');
  }
}

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

@Component({
  selector: 'app-parent',
  template: `
    <app-child></app-child>
    <button (click)="callChildMethod()">Call Child</button>
  `
})
export class ParentComponent implements AfterViewInit {
  @ViewChild(ChildComponent) child!: ChildComponent;
  
  ngAfterViewInit() {
    console.log(this.child.message);
  }
  
  callChildMethod() {
    this.child.showAlert();
  }
}
You should see
(The parent can directly access the child component's properties and methods)

Real-world usage

How to apply it in a real project

Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.

Mini challenge

Build it yourself

Rebuild the ViewChild & ViewChildren example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
// child.component.ts
export class ChildComponent {
  message = 'Hello from Child!';
  
  showAlert() {
    alert('Child method called!');
  }
}

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

@Component({
  selector: 'app-parent',
  template: `
    <app-child></app-child>
    <button (click)="callChildMethod()">Call Child</button>
  `
})
export class ParentComponent implements AfterViewInit {
  @ViewChild(ChildComponent) child!: ChildComponent;
  
  ngAfterViewInit() {
    console.log(this.child.message);
  }
  
  callChildMethod() {
    this.child.showAlert();
  }
}

Expected: Explain the core concept and Angular data flow of ViewChild & ViewChildren

Show hint

Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.

Quick quiz 1 / 3

What best describes the core idea of ViewChild & ViewChildren?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • input and state → component and template → rendered Angular UI
  • View queries retrieve a child component, directive, or template reference from a component's rendered view. Availability depends on view initialization and conditional rendering, so lifecycle and query semantics matter. Prefer inputs and outputs for ordinary communication; reserve ViewChild for focus, measurement, and imperative third-party APIs.
  • A parent calling many child methods through ViewChild breaks component encapsulation.
  • Use ViewChild to focus an input when a dialog opens and test the query before and after conditional rendering.

Angular — Components — Angular

Easy traps

  • A parent calling many child methods through ViewChild breaks component encapsulation.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the ViewChild & ViewChildren example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (The parent can directly access the child component's properties and methods)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

ViewChild & ViewChildren | Thuta Learning