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

203
Views
Llamar a un método del componente hijo

Tengo un componente secundario anidado como este:

 <app-main> <child-component /> </app-main>

Mi componente appMain necesita invocar un método en el componente secundario.

¿Cómo invocar un método en el componente secundario?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Puede obtener una referencia a un elemento usando

 @ViewChild('childComponent') child;

donde childComponent es una variable de plantilla <some-elem #childComponent >` o

 @ViewChild(ComponentType) child;

donde ComponentType es el tipo de un componente o directiva y luego en ngAfterViewInit o un controlador de eventos llama a child.someFunc() .

 ngAfterViewInit() { console.log(this.child); }

Ver también hacerse con un elemento de la plantilla

over 4 years ago · Santiago Trujillo Report

0

El padre y el niño pueden comunicarse a través del enlace de datos.

Ejemplo:

 @Component({ selector: 'child-component', inputs: ['bar'], template: `"{{ bar }}" in child, counter {{ n }}` }) class ChildComponent{ constructor () { this.n = 0; } inc () { this.n++; } } @Component({ selector: 'my-app', template: ` <child-component #f [bar]="bar"></child-component><br> <button (click)="f.inc()">call child func</button> <button (click)="bar = 'different'">change parent var</button> `, directives: [ChildComponent] }) class AppComponent { constructor () { this.bar = 'parent var'; } } bootstrap(AppComponent);

Manifestación

#f crea una referencia al componente secundario y se puede usar en la plantilla o pasar a la función. Los datos del padre se pueden pasar mediante enlace [ ] .

over 4 years ago · Santiago Trujillo Report

0

Ser un hijo componente

 @Component({ // configuration template: `{{data}}`, // more configuration }) export class Son { data: number = 3; constructor() { } updateData(data:number) { this.data = data; } }

Tener un componente paterno

 @Component({ // configuration }) export class Parent { @ViewChild(Son) mySon: Son; incrementSonBy5() { this.mySon.updateData(this.mySon.data + 5); } }

En la plantilla del padre

 <son></son> <button (click)="incrementSonBy5()">Increment son by 5</button>

Esta solución solo funciona para una instancia de <son></son> en la plantilla principal. Si tiene más de una instancia, solo funcionará en la primera de la plantilla.

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!