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

279
Views
Cómo llamar al método desde el componente secundario al componente principal en Angular

Tengo un método en el componente Child como el siguiente:

 showAcknowledgement() { this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => { data.data.filter((res: any) => { if (data.isSuccessful === true) { this.noticeText = res.noticeText; this.noticeAttachment = res.noticeAttachement; this.noticeCode = res.noticeCode; } else if (data.isSuccessful === false) { this.message = [{ field: "", message: data.message[0].message }]; this.objError = this.message; this.showError = true; window.scrollTo({ top: 0, behavior: 'smooth' }); } }) }); }

Necesito llamar a este método en el componente principal dentro de ngOnInit() porque mientras se carga la página necesito llamar a ese método. Intenté como a continuación pero no funciona

HTML principal:

 <app-acknowledge-notice-modal #child></app-acknowledge-notice-modal>

Padre TS:

 @ViewChild(AcknowledgeNoticeModalComponent, {static : true}) child : AcknowledgeNoticeModalComponent; ngOnInit() { this.child.showAcknowledgement(); }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Con @Output() dentro del componente secundario ( https://angular.io/guide/inputs-outputs )

No es una llamada de método directo, sino algo bastante común. Entonces, en tu caso podría ser algo como esto:

HTML principal:

 <app-acknowledge-notice-modal (someEvent)=someMethod()></app-acknowledge-notice-modal>

Niño TS:

 @Output() someEvent = new EventEmitter<string>();

someEvent en el HTML de los padres y el TS del niño deben coincidir, mientras que someMethod() es un método dentro del TS de los padres

over 4 years ago · Santiago Trujillo Report

0

¿Puedes intentarlo así?

Uso del selector de tipo:

Componente hijo

 @Component({ selector: 'app-acknowledge-notice-modal', template: <div>...</div> }) class AcknowledgeNoticeModalComponent{ showAcknowledgement() { this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => { data.data.filter((res: any) => { if (data.isSuccessful === true) { this.noticeText = res.noticeText; this.noticeAttachment = res.noticeAttachement; this.noticeCode = res.noticeCode; } else if (data.isSuccessful === false) { this.message = [{ field: "", message: data.message[0].message }]; this.objError = this.message; this.showError = true; window.scrollTo({ top: 0, behavior: 'smooth' }); } }) }); } }

Componente principal

 @Component({ selector: parent-component, template: <app-acknowledge-notice-modal></app-acknowledge-notice-modal>, directives: [AcknowledgeNoticeModalComponent] }) class ParentComponent{ @ViewChild(AcknowledgeNoticeModalComponent) child: AcknowledgeNoticeModalComponent; ngAfterViewInit() { this.child.showAcknowledgement(); } }

Usando el selector de cadena:

Componente hijo

 @Component({ selector: 'app-acknowledge-notice-modal', template: <div>...</div> }) class AcknowledgeNoticeModalComponent{ showAcknowledgement() { this.noticeService.getNotice(ConstUrls.NoticeManagement.GetUserNotice).subscribe((data: any) => { data.data.filter((res: any) => { if (data.isSuccessful === true) { this.noticeText = res.noticeText; this.noticeAttachment = res.noticeAttachement; this.noticeCode = res.noticeCode; } else if (data.isSuccessful === false) { this.message = [{ field: "", message: data.message[0].message }]; this.objError = this.message; this.showError = true; window.scrollTo({ top: 0, behavior: 'smooth' }); } }) }); } }

Componente principal

 @Component({ selector: parent-component, template: <app-acknowledge-notice-modal #child></app-acknowledge-notice-modal>, directives: [AcknowledgeNoticeModalComponent] }) class ParentComponent{ @ViewChild('child') child: AcknowledgeNoticeModalComponent; ngAfterViewInit() { this.child.showAcknowledgement(); } }
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!