How pass data from ModalService into component

MgSam picture MgSam · Feb 7, 2018 · Viewed 15.8k times · Source

I'm trying to use ngx-bootstrap-modal to pass data from a modal service into a modal component. The examples show this:

this.modalService.show(ModalContentComponent, {initialState});

But it doesn't show how the ModalContentComponent actually consumes that state. I've played around in a debugger and I never see my component getting that data.

How do I access it from the component?

Answer

Suhel Khan picture Suhel Khan · Feb 8, 2018

You can also use the BsModalRef content Like

my-modal.component.ts

export class MyModalComponent {
  public myContent;
  constructor(){}
}

calling your modal from some other component

import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';
...
    public modalRef: BsModalRef;

    constructor(public modalService: BsModalService){}

    public openModal() {
       this.modalRef = this.modalService.show(MyModalComponent);
       this.modalRef.content.myContent= 'My Modal Content';
    }
...