angularngx-bootstrap

Pass data from ngx-bootstrap modal which is child component to parent component


I have seperate child component which in ngx-bootstrap modal here i have defined @input @output properties

  @Input() public openFrom;
  @Output() selecteduse = new EventEmitter<any>();

from parent component i'm opening this modal

const modalRef=this.modalServiceNew.show(ListComponent, { class: "model",   ...this.modOpt   })

but need some way to catch some event

previously through ng-bootstrap was doing like this but in ngx-bootstrap facing issues

for sending data :

modalRef.componentInstance.openFrom="form name";

for receiving event:

modalRef.componentInstance.selecteduse.subscribe(($e) => {
   thid.func1();
})

enter image description here

Any solution Thanks


Solution

  • If you check the docs there is a property called content which you can use to achieve the same.

    This content exists only if you open a component, it will be null, if you open with a ng-template.

    import { Component, OnInit } from '@angular/core';
    import { BsModalRef, BsModalService, ModalOptions } from 'ngx-bootstrap/modal';
     
    @Component({
      // eslint-disable-next-line @angular-eslint/component-selector
      selector: 'demo-modal-service-component',
      templateUrl: './service-component.html'
    })
    export class DemoModalServiceFromComponent {
      bsModalRef?: BsModalRef;
      constructor(private modalService: BsModalService) {}
     
      openModalWithComponent() {
        const initialState: ModalOptions = {
          initialState: {
            list: ['Open a modal with component', 'Pass your data', 'Do something else', '...'],
            title: 'Modal with component'
          }
        };
        this.bsModalRef = this.modalService.show<ModalContentComponent>(ModalContentComponent, initialState);
        this.bsModalRef.content.openFrom="form name";
        this.bsModalRef.content.selecteduse.subscribe(($e) => {
           thid.func1();
        })
      }
    }