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

158
Views
How can i call the same child method when i have the list of same child component in parent component in angular

This is parent.html

<child *ngFor="let detail of listContact; let i = index" 
        [detailsItem]="detail" [index]="i">
</child>

This is child.compenent

export class ChildComponent implements OnInit {
  @Input() index: number;
  @Input() detailsItem: any;
  constructor() {
  }
  ngOnInit() {

  }
  saveChild(){
console.log('index', this.index);
  }
}

And I want to call the method saveChild in parentComponent like this:

export class ParentComponent implements OnInit {
  private child: ChildComponent;
  @Input() index: number;
  @Input() detailsItem: any;
  constructor() {
  }
  ngOnInit() {
  }
  save(){
    this.child.saveChild();
  }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can use @ViewChild to run that.

import { AfterViewInit, Component, ViewChild } from '@angular/core';
import { ChildComponent } from '../child/child.component';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css']
})
export class ParentComponent implements AfterViewInit {
  @ViewChild(ChildComponent) public child!:ChildComponent

  constructor() { }

  ngAfterViewInit(): void {
    this.child.childMethod();
  }

}

It's going to let you access the properties and methods inside the ChildComponent. However remember to use ngAfterViewInit so that the child is initialised when you try and access it.

about 4 years ago · Juan Pablo Isaza 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!