The @Input decorator lets a parent component send data to its child component. It helps components share information easily.
@Input decorator for parent to child in Angular
import { Component, Input } from '@angular/core'; @Component({ selector: 'child-component', template: `{{ data }}` }) export class ChildComponent { @Input() data: string = ''; }
The @Input() decorator marks a property to receive data from the parent.
The property name inside @Input() is optional if it matches the parent binding name.
title receives a string from the parent.export class ChildComponent { @Input() title: string = ''; }
parentName, but the child uses childName internally.export class ChildComponent { @Input('parentName') childName: string = ''; }
The parent component AppComponent passes the string parentMessage to the child component ChildComponent using the @Input property message. The child displays this message.
import { Component, Input } from '@angular/core'; @Component({ selector: 'child-comp', template: `<p>Message from parent: {{ message }}</p>` }) export class ChildComponent { @Input() message: string = ''; } @Component({ selector: 'app-root', template: `<child-comp [message]="parentMessage"></child-comp>` }) export class AppComponent { parentMessage = 'Hello from Parent!'; }
Always initialize @Input properties to avoid undefined errors.
Changes to the input property update the child view automatically.
Use @Input only for data coming from the parent, not for child internal state.
@Input lets a child component receive data from its parent.
Use it to make components communicate and share data easily.
It keeps components reusable and controlled by their parents.
Practice
@Input decorator in Angular?Solution
Step 1: Understand the role of
The@Input@Inputdecorator is used to mark a property in a child component that can receive data from its parent.Step 2: Compare options with the concept
Only To allow a child component to receive data from its parent component correctly describes this data flow from parent to child. Options B, C, and D describe other unrelated Angular features.Final Answer:
To allow a child component to receive data from its parent component -> Option DQuick Check:
@Inputpasses data parent to child [OK]
@Input means data flows into child [OK]- Confusing
@Inputwith@Output - Thinking
@Inputsends data up to parent - Mixing
@Inputwith services or routing
@Input property named title in a child component?Solution
Step 1: Recall correct
In Angular, to declare an input property, you use@Inputsyntax@Input()decorator before the property declaration.Step 2: Check each option
@Input() title: string; correctly uses@Input()with the property name and type. Options A and D miss the decorator syntax, and C uses@Output()which is for event emitters, not inputs.Final Answer:
@Input() title: string; -> Option BQuick Check:
Decorator syntax is@Input()[OK]
@Input() decorator before property [OK]- Using
@Outputinstead of@Input - Forgetting parentheses after
@Input - Writing 'input' lowercase without decorator
@Component({ selector: 'child-comp', template: '{{name}}' })
export class ChildComponent {
@Input() name!: string;
}And the parent template:
<child-comp [name]="parentName"></child-comp>
If
parentName = 'Angular', what will the child component display?Solution
Step 1: Understand data flow from parent to child
The parent passes the string 'Angular' to the child'snameproperty using property binding[name].Step 2: Check child template rendering
The child template displays{{name}}, which will be replaced by the value passed, 'Angular'.Final Answer:
Angular -> Option AQuick Check:
Parent value passed to child input = displayed text [OK]
@Input [OK]- Thinking the literal string 'parentName' is displayed
- Expecting template syntax
{{name}}to show literally - Assuming error because of missing initialization
@Component({ selector: 'child-comp', template: '{{data}}' })
export class ChildComponent {
@Input() data: string;
}And parent template:
<child-comp data="parentData"></child-comp>
What is the problem here?
Solution
Step 1: Identify how parent passes data to child input
To pass a variable value from parent to child, Angular requires property binding syntax with square brackets, e.g.,[data].Step 2: Analyze the given parent template
The parent usesdata="parentData"without brackets, which passes the literal string 'parentData', not the variable's value.Final Answer:
The parent should use property binding with square brackets:[data]-> Option CQuick Check:
Use[inputProp]to pass variables [OK]
@Input [OK]- Passing variable names as strings without brackets
- Confusing
@Inputwith@Output - Thinking default initialization fixes binding errors
user = {name: 'Alice', age: 30}. You want to pass only the name to a child component using @Input. Which is the best way to do this in the parent template?Solution
Step 1: Understand the data to pass
The parent wants to pass only thenameproperty from theuserobject, not the whole object.Step 2: Check each option for correct binding
<child-comp [userName]="user.name"></child-comp> correctly bindsuser.nameto the child'suserNameinput. <child-comp [userName]="user"></child-comp> passes the whole object, not just the name. <child-comp userName="user.name"></child-comp> passes the string literal 'user.name' instead of the value. <child-comp [userName]="user['age']"></child-comp> passes the age, not the name.Final Answer:
<child-comp [userName]="user.name"></child-comp> -> Option AQuick Check:
Bind exact property with[inputProp]="object.prop"[OK]
[inputProp]="object.prop" [OK]- Passing whole object instead of specific property
- Using quotes without brackets causing string literal
- Passing wrong property like age instead of name
Ready to prove it?
Pass the test and this pattern moves unknown
