Search code examples
angulartypescriptng-bootstrap

Refactor observable to call a NgbModal in a service


I have a service in angular 11 that use to call a Ngbmodal component to subscribe when i closed that component. This is the code:

  showMessage(messageData: MessageDataDTO): Observable<MessageResult> {
    return new Observable((result) => {
      const dialogRef = this.dialog.open(
        MessageComponent,
        this.ngbModalOptions
      );
      dialogRef.componentInstance.data = messageData;
      dialogRef.closed.subscribe((res) => {
        result.next(res);
        result.complete();
      });
    });
  }

How could I refactor the code for omit the result.next(res); result.complete();?


Solution

  • @Clashsoft upss you're right. I refactored the code to this:

    showMessage(messageData: MessageDataDTO): Observable<MessageResult> {
      const dialogRef = this.dialog.open(MessageComponent, this.ngbModalOptions);
      dialogRef.componentInstance.data = messageData;
      return dialogRef.closed;
    }
    

    Solved.