Table of contents
1.
Introduction
2.
@Input() and @Output() Properties
3.
How to Use @Input()?
3.1.
Example of @Input() in Angular
3.2.
Step 1: Define an @Input() property in the Child Component
3.3.
Step 2: Pass data from the Parent Component
4.
How to Use @Output()?
4.1.
Example of @Output() in Angular
4.2.
Step 1: Define an @Output() Event Emitter in the Child Component
4.3.
Step 2: Handle the Event in the Parent Component
5.
@Input() and @Output() Together
5.1.
Example of using @Input() and @Output() together
6.
@Input() and @Output() Declarations
6.1.
Declaring @Input()
6.2.
Declaring @Output()
7.
Aliasing Inputs and Outputs
7.1.
Example
7.2.
Usage in Parent Component
8.
How Components Interact in Angular?  
8.1.
What are Input & Output Properties?  
9.
Example: Passing Data from Parent to Child (Input)  
9.1.
Step 1: Create the Child Component  
9.2.
Step 2: Define the Input Property in the Child Component  
9.3.
Step 3: Pass Data from the Parent Component  
10.
Example: Sending Data from Child to Parent (Output)  
10.1.
Step 1: Define the Output Property in the Child Component  
10.2.
Step 2: Handle the Output in the Parent Component  
10.3.
Why is This Important?  
11.
Frequently Asked Questions
11.1.
What is the difference between @Input() and @Output() in Angular?
11.2.
Can I use both @Input() and @Output() in the same component?
11.3.
How do I pass multiple values using @Input()?
12.
Conclusion
Last Updated: Feb 12, 2025
Easy

Input and Output Directives in Angular

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Input and Output Directives in Angular are used for communication between parent and child components. The @Input() directive allows a parent component to pass data to a child component, while the @Output() directive enables the child component to send data or events back to the parent, usually via an event emitter. These directives help manage data flow and make components more interactive.

Input and Output Directives in Angular

In this article, we will learn how to use @Input() and @Output() effectively, along with practical examples and code snippets.

@Input() and @Output() Properties

In Angular, components often need to communicate with each other. The @Input() and @Output() decorators are used to achieve this communication.

  • @Input(): Used to pass data from a parent component to a child component.
     
  • @Output(): Used to send events or data from a child component to a parent component.

These properties help in creating modular, reusable components by establishing a structured way of sharing data.

How to Use @Input()?

Example of @Input() in Angular

@Input() allows data flow from a parent component to a child component.

Step 1: Define an @Input() property in the Child Component

child.component.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `<h3>Welcome, {{ name }}!</h3>`
})
export class ChildComponent {
  @Input() name!: string;
}

Step 2: Pass data from the Parent Component

parent.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `<app-child [name]="userName"></app-child>`
})
export class ParentComponent {
  userName: string = 'John Doe';
}

 

Output:

Welcome, John Doe!

 

Here, the userName from the ParentComponent is passed to the ChildComponent using @Input().

How to Use @Output()?

@Output() is used to send data or events from a child component to a parent component.

Example of @Output() in Angular

Step 1: Define an @Output() Event Emitter in the Child Component

child.component.ts

import { Component, Output, EventEmitter } from '@angular/core';
@Component({
  selector: 'app-child',
  template: `<button (click)="sendMessage()">Click Me</button>`
})
export class ChildComponent {
  @Output() messageEvent = new EventEmitter<string>();
  
  sendMessage() {
    this.messageEvent.emit('Hello from Child Component!');
  }
}

Step 2: Handle the Event in the Parent Component

parent.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'app-parent',
  template: `<app-child (messageEvent)="receiveMessage($event)"></app-child>
             <p>{{ message }}</p>`
})
export class ParentComponent {
  message: string = '';

  receiveMessage(event: string) {
    this.message = event;
  }
}

 

Output:

(Button is clicked)
Hello from Child Component!

 

This shows how @Output() helps send data from a child component to a parent component.

@Input() and @Output() Together

Both @Input() and @Output() can be used together to establish two-way communication between components.

Example of using @Input() and @Output() together

child.component.ts

import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    <h3>Welcome, {{ name }}!</h3>
    <button (click)="sendMessage()">Send Greeting</button>
  `
})
export class ChildComponent {
  @Input() name!: string;
  @Output() messageEvent = new EventEmitter<string>();
  
  sendMessage() {
    this.messageEvent.emit(`Hello, ${this.name}!`);
  }
}

 

parent.component.ts

import { Component } from '@angular/core';
@Component({
  selector: 'app-parent',
  template: `
    <app-child [name]="userName" (messageEvent)="receiveMessage($event)"></app-child>
    <p>{{ message }}</p>
  `
})
export class ParentComponent {
  userName: string = 'John Doe';
  message: string = '';

  receiveMessage(event: string) {
    this.message = event;
  }
}

 

Output:

Welcome, John Doe!
(Button is clicked)
Hello, John Doe!

 

This demonstrates both @Input() and @Output() in action together.

@Input() and @Output() Declarations

Declaring @Input()

@Input() propertyName: type;

 

Example:

@Input() userAge!: number;

Declaring @Output()

@Output() propertyName = new EventEmitter<type>();


Example:

@Output() userUpdated = new EventEmitter<string>();

Aliasing Inputs and Outputs

We can alias inputs and outputs by passing a string argument to @Input() and @Output().

Example

@Input('customName') name!: string;
@Output('customEvent') messageEvent = new EventEmitter<string>();

Usage in Parent Component

<app-child [customName]="userName" (customEvent)="receiveMessage($event)"></app-child>

 

This allows us to use different names in the parent and child components.

How Components Interact in Angular?  

In Angular, components are the building blocks of an application. They are responsible for managing a specific part of the user interface. For an application to work smoothly, components need to communicate with each other. This communication happens through Input & Output properties.  

What are Input & Output Properties?  

  • Input: Used to pass data from a parent component to a child component.  
     
  • Output: Used to send data from a child component to a parent component.  

Let’s understand this with an example.  

Example: Passing Data from Parent to Child (Input)  

Imagine you have a parent component (`AppComponent`) & a child component (`UserComponent`). You want to pass a user’s name from the parent to the child.  

Step 1: Create the Child Component  

First, generate the child component using Angular CLI:  

ng generate component user

Step 2: Define the Input Property in the Child Component  

In the `user.component.ts` file, define an input property to receive the data:  

import { Component, Input } from '@angular/core';
@Component({
  selector: 'app-user',
  template: `<p>Welcome, {{ userName }}!</p>`
})
export class UserComponent {
  @Input() userName: string; // This will receive data from the parent
}

Step 3: Pass Data from the Parent Component  

In the `app.component.ts` file, pass the data to the child component:  

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  template: `
    <h1>User Details</h1>
    <app-user [userName]="name"></app-user> <!-- Passing data to the child -->
  `
})
export class AppComponent {
  name = 'John Doe'; // Data to be passed
}


Here, the `userName` property in the child component receives the value `John Doe` from the parent.  

Example: Sending Data from Child to Parent (Output)  

Now, let’s say the child component needs to send a message back to the parent.  

Step 1: Define the Output Property in the Child Component  

In the `user.component.ts` file, define an output property using `EventEmitter`:  

import { Component, Output, EventEmitter } from '@angular/core';
@Component({
  selector: 'app-user',
  template: `
    <p>Welcome, {{ userName }}!</p>
    <button (click)="sendMessage()">Send Message</button>
  `
})
export class UserComponent {
  @Input() userName: string;
  @Output() messageEvent = new EventEmitter<string>(); // Output property
  sendMessage() {
    this.messageEvent.emit('Hello from the child!'); // Emitting data
  }
}

Step 2: Handle the Output in the Parent Component  

In the `app.component.ts` file, handle the emitted event:  

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
  template: `
    <h1>User Details</h1>
    <app-user [userName]="name" (messageEvent)="receiveMessage($event)"></app-user>
    <p>{{ message }}</p>
  `
})
export class AppComponent {
  name = 'John Doe';
  message: string;

  receiveMessage($event: string) {
    this.message = $event; // Receiving data from the child
  }
}


Here, when the button in the child component is clicked, it sends a message to the parent, which is then displayed.  

Why is This Important?  

Input & output properties make Angular components reusable & modular. By passing data between components, you can build complex applications with clear separation of concerns.  

Frequently Asked Questions

What is the difference between @Input() and @Output() in Angular?

@Input() is used to pass data from a parent component to a child component, whereas @Output() is used to send data from a child component to a parent component.

Can I use both @Input() and @Output() in the same component?

Yes, you can use both in a component to allow two-way communication between the parent and child components.

How do I pass multiple values using @Input()?

You can pass multiple values by defining multiple @Input() properties and binding them in the template.

Conclusion

In this article, we learned about Input and Output Directives in Angular and how they help components communicate with each other. The @Input() decorator allows data to flow into a component, while @Output() sends data from one component to another. Understanding these directives is important for creating dynamic and interactive Angular applications.

Live masterclass