Thuta Learning
Angular
IntermediateWeb DevelopmentIntermediate

@Input() - Parent to Child

AngularLesson 1820 min

What you'll walk away with

  • Explain the core concept and Angular data flow of @Input() - Parent to Child
  • 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

@Input() - Parent to Child, in plain language

@Input() decorator is used to pass data from a parent component down to a child component.

Problem → Why → Solution

The problem — why this concept matters

Mutating an input object inside the child can unexpectedly alter parent state.

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

An input is a public API that carries parent-owned data one way into a child. Define required and optional contracts, defaults, transformations, and an immutable update policy; the child should not treat the input as privately owned mutable state. Updating nested fields without a new reference can also defeat optimized change detection.

Mental model
  1. user input
  2. binding and validation
  3. valid state or feedback

Explain the core concept and Angular data flow of @Input() - Parent to Child

Core syntax or pattern

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

@Component({
  selector: 'app-child',
  template: `
    <div class="child">
      <h3>{{ title }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ChildComponent {
  @Input() title: string = '';
  @Input() age: number = 0;
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child [title]="userName" [age]="userAge"></app-child>
  `
})
export class ParentComponent {
  userName = 'John Doe';
  userAge = 25;
}

An input is a public API that carries parent-owned data one way into a child. Define required and optional contracts, defaults, transformations, and an immutable update policy; the child should not treat the input as privately owned mutable state. Updating nested fields without a new reference can also defeat optimized change detection.

Read the code step by step

  1. user input// child.component.ts

    @Input() decorator is used to pass data from a parent component down to a child component.

  2. binding and validation@Input() age: number = 0;

    An input is a public API that carries parent-owned data one way into a child. Define required and optional contracts, defaults, transformations, and an immutable update policy; the child should not treat the input as privately owned mutable state. Updating nested fields without a new reference can also defeat optimized change detection.

  3. valid state or feedback}

    Create a reusable product card with a required product input and optional compact mode, then verify an immutable parent update refreshes the UI.

Execution flow
  1. user input
  2. binding and validation
  3. valid state or feedback

Success

Explain the core concept and Angular data flow of @Input() - Parent to Child

Failure

Mutating an input object inside the child can unexpectedly alter parent state.

@Input() decorator is used to pass data from a parent component down to a child component.

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

@Component({
  selector: 'app-child',
  template: `
    <div class="child">
      <h3>{{ title }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ChildComponent {
  @Input() title: string = '';
  @Input() age: number = 0;
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child [title]="userName" [age]="userAge"></app-child>
  `
})
export class ParentComponent {
  userName = 'John Doe';
  userAge = 25;
}
You should see
(The child component receives 'John Doe' and 25 from the parent and displays them)

Real-world usage

How to apply it in a real project

Create a reusable product card with a required product input and optional compact mode, then verify an immutable parent update refreshes the UI.

Mini challenge

Build it yourself

Rebuild the @Input() - Parent to Child 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
import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <div class="child">
      <h3>{{ title }}</h3>
      <p>Age: {{ age }}</p>
    </div>
  `
})
export class ChildComponent {
  @Input() title: string = '';
  @Input() age: number = 0;
}

// parent.component.ts
@Component({
  selector: 'app-parent',
  template: `
    <app-child [title]="userName" [age]="userAge"></app-child>
  `
})
export class ParentComponent {
  userName = 'John Doe';
  userAge = 25;
}

Expected: Explain the core concept and Angular data flow of @Input() - Parent to Child

Show hint

Create a reusable product card with a required product input and optional compact mode, then verify an immutable parent update refreshes the UI.

Quick quiz 1 / 3

What best describes the core idea of @Input() - Parent to Child?

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

  • user input → binding and validation → valid state or feedback
  • An input is a public API that carries parent-owned data one way into a child. Define required and optional contracts, defaults, transformations, and an immutable update policy; the child should not treat the input as privately owned mutable state. Updating nested fields without a new reference can also defeat optimized change detection.
  • Mutating an input object inside the child can unexpectedly alter parent state.
  • Create a reusable product card with a required product input and optional compact mode, then verify an immutable parent update refreshes the UI.

Angular — Components — Angular

Easy traps

  • Mutating an input object inside the child can unexpectedly alter parent state.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the @Input() - Parent to Child 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 child component receives 'John Doe' and 25 from the parent and displays them)

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.

@Input() - Parent to Child | Thuta Learning