Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 963
Greg Avatar asked Aug 28 '17 06:08

Greg


1 Answers

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).

like image 58
Vasiliy Mazhekin Avatar answered Sep 19 '22 16:09

Vasiliy Mazhekin