Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

492
Views
Angular: How to interact components from Parent component to child component

Have two components,one is top panel(parent) and and display(child) components. I tried to enter some values like firstname, lastname, email and click on button to display entered values in child component space(eg: Andy John test@test.com). Could you please help me how to interaction between parent component to child component while clicking the button.

Here is the Example

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

In your code there was some mistakes

  1. You didn't place any Selector ie: <app-child [parentOutputVar]="parentVar">
  2. didnt write how to display values in child.

For more details refer :

-> https://angular.io/guide/inputs-outputs

-> https://angular.io/guide/component-interaction

For your purpose please check your edited example: https://stackblitz.com/edit/angular7-communication-between-components-coqvnq?file=src%2Fapp%2Fchild%2Fchild.component.html using @Input directive

Explanation

Here in parent components are changed to form with formcontrols -firstname, lastname, email etc. while clicking the button the form values are added into array-parentVar using .push. Here in parent.componet.html, we have added childcomponet directive as

<app-child [parentOutputVar]="parentVar"></app-child>

The parentVar in parent.component sents data to app-child as input ([parentOutputVar]="parentVar").

The Data will displayed using structural directives -ngIf &ngFor

over 4 years ago · Santiago Trujillo Report

0

You can add properties to the child component like this:

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

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
  @Input() firstname: string;
}

Communicate them down from the parent's html template like this:

<app-child [firstname]="firstname"></app-child>

And display them in the child's html template like this:

Hello {{firstname}}
over 4 years ago · Santiago Trujillo Report

0

For sharing data between parent and child components, we use the @Input() and @Output() decorators.

The first step is to establish the hierarchy between the parent and the child components. The <parent-component> provides context to the <child-component>

Out of the two discussed decorators @Input and @Output the @Input decorator lets a parent component update data in the child component and @Output decorator lets the child send data to the parent component.

Your requirement specifies, sending data to the child component from the parent component on click of the submit button. For which you first need to configure your child component such that,

  • In your child.component.ts, Add the @Input() and decorate the property with it as shown below,
@Input() formData: any = [];
  • In your parent.component.ts, use the child selector <app-child [formData] = "formValue"></app-child> as a directive within its template. Here we are using property binding to bind formValue property of parent to formData property of child.

TLDR;

Please find the StackBlitz project illustrating parent-to-child data communication.

Here I have updated the example you provided to display the values entered in the form inside your child component, using the Input() decorator.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!