Input Decorator In Angular
Last Updated :
03 Sep, 2024
The @Input decorator in Angular is used to pass data from the parent to child components. It allows a parent component to bind data to properties in a child component enabling the sharing of data between components in a hierarchical relationship.
The @Input decorator is important for communication between components ensuring that the child component can receive data from its parent and act accordingly. In this article, we will explain the input decorator in angular with related examples and outputs for your reference.
What is the @Input Decorator?
The @Input decorator is used to define an input property in a child component. This property can receive data passed from a parent component. The @Input decorator binds the data from the parent to the child components property enabling the child component to utilize this data in its template logic.
Why Use the @Input Decorator?
The @Input decorator is essential for building dynamic and flexible components in Angular. It allows parent components to configure child components by passing in data. This decoupling of data and behaviour between components provides better code organization, promotes reusability, and simplifies testing.
Prerequisites
Approach
In this approach you simply declare a property in the child component and decorate it with @Input. This property will be bound to a value from the parent component. When the parent component passes a value to this property the child component can use it directly in its template.
Syntax
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: '<p>{{data}}</p>',
})
export class ChildComponent {
@Input() data: string;
}
In this approach the @Input decorator is used in conjunction with setter function and this allows you to perform custom logic whenever the input value is updated such as transforming the data before storing it in the components state. This is useful when you need to process the input data before displaying or using it in the child component.
Syntax
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: '<p>{{processedData}}</p>',
})
export class ChildComponent {
private _data: string;
@Input()
set data(value: string) {
this._data = value.toUpperCase(); // Custom logic
}
get data(): string {
return this._data;
}
}
Steps To Implement Input Decorator
Step 1 : Install Angular CLI
We have already Angular CLI. If you don't have install by using below command.
npm install -g @angular/cli
Step 2 : Create a New Angular Application
Once Angular CLI is installed Now we need create a new angular application by using below command.
ng new input-decorator-demo
Step 3 : Navigate to Project Folder
cd input-decorator-demo
Folder Structure
Folder StructureStep 4 : Generate Components
ng generate component parent
ng generate component child
Step 5 : Modify the Parent and Child Components
Once Parent component is created. Now imports the child component into the parent component by using imports keyword in angular. And we define a message Hello from Parent in the parent component.
JavaScript
//src/app/parent/parent.component.ts
import { Component } from '@angular/core';
import { ChildComponent } from '../child/child.component';
@Component({
selector: 'app-parent',
template: '<app-child [data]="parentData"></app-child>',
standalone: true,
imports: [ChildComponent], // Import ChildComponent
})
export class ParentComponent {
parentData: string = 'Hello from Parent';
}
child.component.ts (approach 1)
In the child component we use input decorator by using @Input with displaying data in the form of paragraph tag. And child component should be standalone true.
JavaScript
//src/app/child/child.component.ts
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-child',
template: '<p>{{data}}</p>', // Display data
standalone: true,
})
export class ChildComponent {
@Input() data!: string; // Input property
}
Step 6 : Update app.component.html to use the Parent Component
Once logic is developed now its time to update the logic for app component file to display the input decorator data. Here we imports the parent component in to application component.
JavaScript
// src/app/app.component.ts
import { Component } from '@angular/core';
import { ParentComponent } from './parent/parent.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: true,
imports: [ParentComponent],
})
export class AppComponent { }
app.component.html
Here we change the application html file. In this file we create a custom html tags. These tags are coming from the application component file.
HTML
<!-- src/app/app.component.html -->
<app-parent></app-parent>
Step 7: Run the Application
ng serve
Output
Input Decorator In Angular
Similar Reads
What are decorators in Angular?
In Angular decorators are important for defining and configuring various application elements, providing an easy way to enhance the classes with additional functionality and provide metadata. In this article, we'll take a detailed look at the concept of Decorators in Angular. Table of Content What a
4 min read
Angular ViewChildren Decorator
The ViewChildren decorator in Angular is used to query multiple elements or directives in the view DOM and gain a reference to them. It is particularly useful when you want to interact with multiple elements of the same type or group within a template. The ViewChildren decorator returns a QueryList
6 min read
HostListener Decorators in Angular 17
Decorators are declarative functions in Angular that are used to change classes, methods, or properties. They serve as a source of metadata that instructs Angular on how to handle a class. What is HostListener Decorator?The @HostListener decorator in Angular provides a convenient way to listen for e
3 min read
Purpose of NgModule Decorator in Angular
The NgModule decorator in Angular is like a blueprint for organizing and configuring different parts of your application. It's like a set of instructions that tells Angular how to assemble the various components, directives, pipes, and services into cohesive units called modules. These modules help
5 min read
How To Use @Injectable Decorator In Angular?
In Angular, the @Injectable decorator is used to mark a class as available for dependency injection. This allows Angular to create and manage instances of this class and inject it into other components, services, or other classes.In this article, we will see how to use the @Injectable decorator in a
3 min read
Creating An Interface In Angular
In Angular, an interface is a TypeScript feature that defines the shape or structure of an object. It helps developers enforce type safety by specifying what properties and types an object must have. Interfaces do not generate JavaScript code but are used during the development phase to ensure that
4 min read
AngularJS ng-paste Directive
The ng-paste Directive in AngularJS is used to specify custom behavior functions when the text in input fields is pasted into an HTML element. It can be used to call a function that will be triggered when the text is pasted into the input field. It is supported by <input>, <select> and
2 min read
Pipes in Angular
Pipes are often used for formatting dates, numbers, and Strings and they can be customized to suit various needs. In this article, we explain about how to use pipes in Angular with examples and related outputs for your reference.Prerequisites:To understand this article you should have knowledge of t
8 min read
Angular 10 NgPlural Directive
In this article, we are going to see what is NgPlural in Angular 10 and how to use it. The NgPlural in Angular10 is used to Add or remove DOM sub-trees based on a numeric value. Syntax: <li *NgPlural='condition'></li> NgModule: Module used by NgPlural is: CommonModule  Selectors: [NgPlu
1 min read
Angular PrimeNG Form Inputtext Icons Component
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will learn how to use the InputText Icons Component in Angular PrimeNG. Letâs le
3 min read