In this article, I am exploring two very important points, related to the Angular 2 + version, which the part of the Parameter Decorator, and these points are called @Input and @Output decorators. Both are used to transform the data from one component to another component. Or, you can say pass the different types of data form parent to child component and child to parent component.
Or
In a simple way, transform/exchange data between two components.
Introduction
In this article, I am exploring two very important points, related to the Angular 2 + version, which the part of the Parameter Decorator, and these points are called @Input and @Output decorators. Both are used to transform the data from one component to another component. Or, you can say pass the different types of data from parent to child component and child to parent component. Or, in a simple way transform/exchange data between two-component.
Let's explore each, one by one.
@Input Decorator
@Input is a decorator to mark a property as an input. @Input is used to define an input property, to achieve component property binding. @Inoput decorator is used to pass data (property binding) from parent to child component. The component property should be annotated with @Input decorator to act as input property.
Let's explore it practically.
I have created an angular application which is AngApp. I have created two components. They are app components and student components. I will transfer the data from the parent to the child component, using @Input decorator. I am assuming my, app-component is the parent component and student-component is the child component.
To make the parent-child relation, keep the instance (selector of student component) of the student component inside the template URL (app.component.html) of the app component.
Open app.component.html: Inside this file, we keep an instance of the student component.
Example
<div>
<app-student></app-student>
</div>

Listing 1.0
In the above image, the selected area is the child component.
Now, we want to send the message from parent to child component.
Let's open the parent component's .ts file (app.component.ts) and declare a variable inside the AppComponent class, to store the message. This message is received by the child component.
myInputMessage: string = "I am the parent component";

import { Component, Input, OnInit } from '@angular/core';
@Component({
selector: 'app-student',
templateUrl: './student.component.html',
styleUrls: ['./student.component.css']
})
export class StudentComponent implements OnInit {
@Input() myinputMsg: string;
constructor() { }
ngOnInit() {
console.log(this.myinputMsg);
}
}
In the above image, we have declared a variable( myInputMessage) shown in the selected area.
Now, let's open parent component views (app.component.html) and pass this variable inside the child component instance, which is passed inside the parent component.
<div>
<app-student [myinputMsg]="myInputMessage"></app-student>
</div>

The above image represents 2 points. Let's explain each of the points.
- Denotes those variables that will be used by the child component (student component) with @Input decorator to fetch the message from the parent component and
- denotes those variables that are passed the parent component message to the child component.
Now, open the child component's .ts file (student.component.ts) and import the Input decorator, using the myinputMsg variable with @Input decorator and print it inside the constructor or ngOnInit().







Jayesh PatanvadiyaPosted Oct 1, 2021, 7:05 AM
Nice article
Sandip G PatilPosted Jul 15, 2021, 10:56 AM
Nice article.. well explained