@InputAngular Inputs Parent ➝ Child Data Flow
In Angular, data is commonly passed from a parent component to a child component using the @Input decorator. This lets the child receive values and use them in its own template or logic.
You define an @Input property in the child, then bind a parent property to it using [propertyName]="parentValue" syntax in the parent template.
@Input decorator – marks a child property as bindable from its parent.[childProp]="parentProp" in its template.Step 1: Import and apply @Input in the child component.
Step 2: Bind a parent property to the child’s @Input in the parent template.
@Input
// child.component.ts - receives data from the parent using @Input
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: '<p>Message from Parent: {{ message }}</p>',
})
export class ChildComponent {
@Input() message: string = '';
}
// parent.component.ts - sends data to the child via property binding
import { Component } from '@angular/core';
@Component({
selector: 'app-parent',
template: '<app-child [message]="parentMessage"></app-child>',
})
export class ParentComponent {
parentMessage: string = 'Hello from Parent!';
}
ChildComponent, the property message is decorated with @Input(). This tells Angular that message can receive a value from a parent.ParentComponent, the template uses [message]="parentMessage" to bind the parent’s parentMessage value to the child’s message property.parentMessage changes in the parent, the value shown in the child’s template ({{ message }}) updates automatically.If the parent has:
parentMessage = 'Hello from Parent!';
Then the child’s rendered HTML will look like:
Message from Parent: Hello from Parent!
@Input properties (e.g., = '') to avoid undefined issues.@Input is one-way data flow; if the child needs to notify the parent, use @Output with EventEmitter.@Input data directly in the child if it’s meant to be read-only; prefer treating inputs as immutable.@Input and displays them in a bullet list.parentMessage and observe how the child’s message updates in real time.