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.
- user input
- binding and validation
- valid state or feedback
Explain the core concept and Angular data flow of @Input() - Parent to Child
Core syntax or pattern
// 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
- user input
// child.component.ts@Input() decorator is used to pass data from a parent component down to a child component.
- 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.
- 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.
- user input
- binding and validation
- 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.
// 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;
}(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
// 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
Quick quiz 2 / 3
Quick quiz 3 / 3
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