DEV Community

Nando AM
Nando AM

Posted on

2 2

ngx-smart-modal 'onDataAdded' called many times

I'm new using angular8 and the library ngx-smart-modal to make a list of clients each one with one button to remove him from the list. When I click on the button I show a confirm modal and when click on accept, passes the client code to another compoment wich search that code and remove the affected index from the array list.

Problem is: in the first modal opened, 'onDataAdded' is called once (expected behavior). In the second is called twice (not expected) and so on...

When I click on 'delete', it triggers this function from 'clients' component which opens the confirmation modal and waits for response (onDataAdded):

delClientModal( index: number ) {
  this.clientToRemove = this.clients[index];
  this.clientService._passClient(this.clients[index]);
  this.ngxSmartModalService.getModal('delClientModal').open();
  this.ngxSmartModalService.get('delClientModal').onDataAdded.subscribe((data: any) => {
    const indexToRemove = this.clients.findIndex(i => i.clientcode === data);
    if (indexToRemove >=  0) {
      this.clients.splice(this.clients.findIndex(i => i.clientcode === data), 1);
    }
  });
}
Enter fullscreen mode Exit fullscreen mode

When I click on accept, it triggers this function from 'client' component wich send the client code to clientService (for api operations) and set the data to de modal for 'onDataAdded' to catch it:

delClient(clientcode: string) {
    this.clientService._deleteClient(this.client);
    this.ngxSmartModalService.setModalData(clientcode, 'delClientModal');
    this.ngxSmartModalService.resetModalData('delClientModal');
  }
Enter fullscreen mode Exit fullscreen mode

What I'm not getting right?

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (1)

Collapse
 
lakshmitj profile image
lakshmitj

I have same issue . did you find a solution for this?

Postgres on Neon - Get the Free Plan

No credit card required. The database you love, on a serverless platform designed to help you build faster.

Get Postgres on Neon

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay