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

246
Visualizações
Why can't I access modal's Output EventEmitter value?

I have a modal component supplied through a service to my parent component. The modal has an output that seems like should trigger an event that passes a string.

I'm just trying to run some code if the modal is confirmed, but I haven't been able to hook into the event that's supposedly emitted. What am I missing?

[ modal.component.ts ]

export class ModalComponent implements OnInit {

  ...
  @Output() onAccepted = new EventEmitter<string>();
  @Output() onDeclined = new EventEmitter<string>();

  @Input() id!: string;
  private element: any;

  constructor(private modalService: ModalService, private el: ElementRef) {
      this.element = el.nativeElement;
  }

  ngOnInit(): void {
    // ensure id attribute exists
    if (!this.id) {
        console.error('modal must have an id');
        return;
    }

    // add self (this modal instance) to the modal service so it's accessible from controllers
    this.modalService.add(this);
  }

  accept()
  {
    this.onAccepted.emit("Accepted");
    this.close();
  }

  ...

  // remove self from modal service when component is destroyed
  ngOnDestroy(): void {
    this.modalService.remove(this.id);
    this.element.remove();
  }

}

[ modal.service.ts ]

@Injectable({ providedIn: 'root' })
export class ModalService {
    private modals: any[] = [];

    add(modal: any) {
        // add modal to array of active modals
        this.modals.push(modal);
    }

    remove(id: string) {
        // remove modal from array of active modals
        this.modals = this.modals.filter(x => x.id !== id);
    }

    open(id: string) {
        // open modal specified by id
        const modal = this.modals.find(x => x.id === id);
        modal.open();
    }

    close(id: string) {
        // close modal specified by id
        const modal = this.modals.find(x => x.id === id);
        modal.close();
    }
}

[modal-parent.component.html]

<modal id="assignAdminModal" title="Assign Administrator role" [message]="'Confirm assigning Administrator rights to this user?'"
  [showDecline]="true" [showAccept]="true" (onAccepted)="test($event)"></frisbo-modal>

[modal-parent.component.ts]

  test(event: any): void {
    console.log(event);
  }
about 4 years ago · Juan Pablo Isaza
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