Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

489
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda