Passing Data from Parent to Child with @Input Decorator in Angular
📖 Scenario: You are building a simple Angular app where a parent component sends a message to its child component. This is common when you want to reuse a child component but show different data inside it.
🎯 Goal: Build two Angular components: a parent and a child. The parent will pass a string message to the child using the @Input decorator. The child will display this message in its template.
📋 What You'll Learn
Create a child component with an @Input property called message of type string
Create a parent component that uses the child component selector in its template
Pass a string value from the parent component to the child component using property binding
Display the passed message inside the child component's template
💡 Why This Matters
🌍 Real World
Passing data from parent to child components is a common pattern in Angular apps to build reusable and dynamic UI parts.
💼 Career
Understanding @Input is essential for Angular developers to create modular components and manage data flow in applications.
Progress0 / 4 steps
1
Create the child component with a message property
Create a child component class called ChildComponent with a public string property called message. Do not add the @Input decorator yet.
Angular
Hint
Define a class with a string property named message initialized to an empty string.
2
Add the @Input decorator to the message property
Import @Input from @angular/core and add the @Input() decorator above the message property in ChildComponent.
Angular
Hint
Use @Input() to mark the message property as input from the parent.
3
Create the parent component and pass a message to the child
Create a parent component class called ParentComponent with a public string property called parentMessage set to 'Hello from Parent'. In the parent component's template, use the child component selector <app-child> and bind parentMessage to the child's message input property.
Angular
Hint
Define parentMessage in the parent class and bind it to the child's message input in the template.
4
Display the message inside the child component template
In the child component's template, add interpolation to display the message property inside a paragraph tag.
Angular
Hint
Use Angular interpolation syntax {{ message }} inside the paragraph tag.
Practice
(1/5)
1. What is the main purpose of the @Input decorator in Angular?
easy
A. To define a route in Angular
B. To send data from a child component to its parent component
C. To create a new Angular service
D. To allow a child component to receive data from its parent component
Solution
Step 1: Understand the role of @Input
The @Input decorator 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 D
Quick Check:
@Input passes data parent to child [OK]
Hint: Remember: @Input means data flows into child [OK]
Common Mistakes:
Confusing @Input with @Output
Thinking @Input sends data up to parent
Mixing @Input with services or routing
2. Which of the following is the correct syntax to declare an @Input property named title in a child component?
easy
A. input title: string;
B. @Input() title: string;
C. @Output() title: string;
D. Input() title: string;
Solution
Step 1: Recall correct @Input syntax
In Angular, to declare an input property, you use @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 B
Quick Check:
Decorator syntax is @Input() [OK]
Hint: Always use @Input() decorator before property [OK]
5. You have a parent component with a property 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?
hard
A. <child-comp [userName]="user.name"></child-comp>
B. <child-comp [userName]="user"></child-comp>
C. <child-comp userName="user.name"></child-comp>
D. <child-comp [userName]="user['age']"></child-comp>
Solution
Step 1: Understand the data to pass
The parent wants to pass only the name property from the user object, not the whole object.
Step 2: Check each option for correct binding
<child-comp [userName]="user.name"></child-comp> correctly binds user.name to the child's userName input. <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 A
Quick Check:
Bind exact property with [inputProp]="object.prop" [OK]
Hint: Bind specific property with [inputProp]="object.prop" [OK]
Common Mistakes:
Passing whole object instead of specific property
Using quotes without brackets causing string literal
Passing wrong property like age instead of name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.