Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
How to access function from parent component when in modal component?

The refreshTable() function is in the parent component. I need to trigger it whenever I updated information in my modal and closes it.

I am using @ng-bootstrap for my modal

For may parent.component.ts

import { NgbModal } from '@ng-bootstrap/ng-bootstrap';

 constructor(
private ngbModal: NgbModal,
)

viewModal() {
    const openModal = this.ngbModal.open(ModalComponent);
    openModal .componentInstance.id = row.id;
}

refreshTable() {
    //refresh code block here
}

For my modal.component.ts

import { NgbActiveModal} from '@ng-bootstrap/ng-bootstrap';

constructor(
private activeModal: NgbActiveModal,
)

updateParent() {
    //update data to database code here
    this.activeModal.close();
}

How to trigger refreshTable() from ModalComponent after closing the modal? Since there are changes in the data from database, data from parent is not updated accordingly when modal is closed.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Try to change to this


const openModal = this.ngbModal.open(ModalComponent);
openModal.componentInstance.id = row.id;
openModal.dismissed.subscribe(
  _ => {
    this.refreshTable();
  }
)

in parent.component.ts

about 4 years ago · Juan Pablo Isaza Relatório

0

Pass it from the parent component to the child component as a prop.

Use bind or an arrow function to preserve the this value.

about 4 years ago · Juan Pablo Isaza Relatório

0

Add an output event to your modal component

@Output() onCLose: EventEmitter<any> = new EventEmitter();

When the user clicks close just call the emit() method

this.onCLose.emit(value); //value can be whatever you want to pass to the parent

Update your viewModal method in the parent to include a subscription to the close event

viewModal() {
   const openModal = this.ngbModal.open(ModalComponent);
   openModal.componentInstance.id = row.id;
   openModal.componentInstance.onClose.subscribe(value => 
      // handle the close event
      this.refreshTable();
   })
}

You could also do it any other way communication between components can be achieved:

  • service
  • state management (ngrx, etc)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda