How to handle the event when click on Angular2-toaster pop-up

7k Views Asked by At

Currently, I'm working on a project that uses angular2-toaster.

// Show message notification
  this.toasterService.pop('success',
    `New message from ${data.sender.name} (${data.sender.hid})`,
    data.message);

When the user clicks to the pop-up, I want to show the dialog for more detail. I've searched the document on https://www.npmjs.com/package/angular2-toaster but can not find the solution to handle the event when user click on the pop-up, can you suggest for me some advice?

Thanks a lot.

2

There are 2 best solutions below

3
On BEST ANSWER

You could use the clickHandler.

@Component({
  selector: 'my-app',
  template: `
    <div>
      <toaster-container [toasterconfig]="config1"></toaster-container>
      <button (click)="popToast()">pop toast</button><br/>
    </div>
  `,
})
export class App {
  private toasterService: ToasterService;

  constructor(toasterService: ToasterService) {
    this.toasterService = toasterService;
  }

  popToast() {
    var toast: Toast = {
      type: 'info',
      title: 'Here is a Toast Title',
      body: 'Here is a Toast Body',
      showCloseButton: true,
      clickHandler: (t, isClosed): boolean => {
        console.log('i am clicked..', isClosed, t);

        // got clicked and it was NOT the close button!
        if (!isClosed) {

        }

        return true; // remove this toast !
      }
    };

    this.toasterService.pop(toast);
  }
}

live-demo: http://plnkr.co/edit/uL98EbfIBd6pm7bMU80V?p=preview

1
On

From the doc, you have the onShowCallback :

var toast: Toast = {
  type: 'success',
  title: 'parent',
  onShowCallback: (toast) => {
    // stuff here
  }  
};

this.toasterService.pop(toast);