ngx-bootstrap modal - access data passed into modal via bsModalRef.content

Greg  picture Greg · Aug 28, 2017 · Viewed 9.1k times · Source

I am using angular 4 and ngx-bootstrap to open modals. All is working well, and I implement modals via a component. I can also pass data into the modal via the content property of the bsModalRef. Like so:

this.bsModalRef = this.modalService.open(MyComponent);
this.bsModalRef.content.somedata = [...some array of products];

This all works great, and my modal can then access a property called somedata. Fantastic. The issue that I'm having is that I want to perform some calculation on somedata. Assume somedata were a list of products with an amount, and I want the modal to reduce the amount down to a single value. How do I know when that data is available in the modal class? I can use a setTimeout hack, which works, but makes me feel icky inside. Is there some kind of lifeCycle hook like ngOnChanges that ngx-bootstrap implements so I can know when a data property gets set, and further, when it changes?

Answer

Vasiliy Mazhekin picture Vasiliy Mazhekin · Sep 1, 2017

You can implement setter, or method

public set somedata(val) {
    // here you process data
}

Keep in mind that this.bsModalRef.content consists all the public members of the dialog component (in your case MyComponent).