Este es padre.html
<child *ngFor="let detail of listContact; let i = index" [detailsItem]="detail" [index]="i"> </child>Este es child.compenent
export class ChildComponent implements OnInit { @Input() index: number; @Input() detailsItem: any; constructor() { } ngOnInit() { } saveChild(){ console.log('index', this.index); } }Y quiero llamar al método saveChild en parentComponent así:
export class ParentComponent implements OnInit { private child: ChildComponent; @Input() index: number; @Input() detailsItem: any; constructor() { } ngOnInit() { } save(){ this.child.saveChild(); } }puede usar @ViewChild para ejecutar eso.
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(); } }Le permitirá acceder a las propiedades y métodos dentro de ChildComponent. Sin embargo, recuerde usar ngAfterViewInit para que el niño se inicialice cuando intente acceder a él.