Angular makes the communication between components very easy. In this article, we will learn how to communicate from a parent component to the child component.
Approach:
-
Let’s create two components:
- parent
- child
- In the parent component, declare the property that you want to receive in the child component, say ‘ParentId’.
-
While including the child component inside the parent component, bind the ‘ParentId’ property to the child component using property binding.
<child [id] ="parentid"></child>chevron_rightfilter_none - Now in the child component import Input from @angular/core and create a property decorated by @input decorator to receive ‘ParentId’ from parent component. The name of property should be the same as used for binding the ‘ParentId’ property i.e ‘id’.
-
‘ParentId’ will be successfully received in the child component as ‘id’.
Example:
-
In this example, we will create a property
'ParentId'and receive it in the child component.
Let’s write the code for the parent component.import { Component } from'@angular/core';@Component({selector:'app-root',// code for parent component view.template:`<div style="text-align: center;"><h1>parent component - {{parentid}}</h1></div>// Bind the ParentId to child component.<child [id] ="parentid"></child>`,styleUrls: []})export class AppComponent {// This property is to be sent to the child component.parentid: number = 1;}chevron_rightfilter_none -
Now write the code for the child component
import { Component, OnInit, Input} from'@angular/core';@Component({selector:'child',template: `<div style="text-align: center;"><h2>child component</h2>// property successfully received from the parent component.parent id is {{id}}</div>`,styles: []})export class TestComponent implements OnInit {// @input decorator used to fetch the// property from the parent component.@Input()id: number;ngOnInit(): void {}}chevron_rightfilter_none
Output:

Recommended Posts:
- Adding Angular Material Component to Angular Application
- jQuery | parent > child Selector
- How to remove parent element except its child element using jQuery ?
- How to get the child element of a parent using JavaScript ?
- How to apply style to parent if it has child with CSS?
- How to call a parent method from child class in JavaScript?
- How to navigate to a parent route from a child route?
- How to enable routing and navigation between component pages in Angular 8 ?
- How to set default values for Angular 2 component properties?
- Routing in Angular JS using Angular UI Router
- Angular CLI | Angular Project Setup
- Angular 7 | Angular Data Services using Observable
- Difference between Angular 4 and Angular 5
- How to find the parent class name with known class in jQuery ?
- HTML | DOM Window parent Property
- How to make flexbox children 100% height of their parent using CSS?
- jQuery | :parent Selector
- jQuery | parent descendant Selector
- JQuery | Get the n-th level parent of an element
- p5.js | parent() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

