javascript - ionic2 - create function only when user clicks "close" inside Toast - Stack Overflow

I created a toast with ionic2 (+ angular2 with javascript, not typescript), where the user removes a it

I created a toast with ionic2 (+ angular2 with javascript, not typescript), where the user removes a item from the list.

So far so good, now I want to add a button Undo (replace Close by Undo) inside the toast to put it back on list and at the same time Dismiss the toast.

My code so far:

archiveItem(item) {
    let toast = Toast.create({
        message: 'Item archived.',
        duration: 2000,
        showCloseButton: true,
        closeButtonText: 'Undo',
        dismissOnPageChange: true,
    });

    var index = this.items.indexOf(item);
    this.items.splice(index, 1); //remove the item


    toast.onDismiss(() => {
        console.log('Dismissed toast');
        this.items.splice(index, 0, item); //put back the item on right place
    });


    this.nav.present(toast);
}

When I click Undo, the item returns to the list, the problem is that if I don't click it, it goes back to the list as well.

I suppose I need to create another function to the Undo, but I don't know how to do that, and Ionic2 DOCS don't talk about it...

Thank you :)

I created a toast with ionic2 (+ angular2 with javascript, not typescript), where the user removes a item from the list.

So far so good, now I want to add a button Undo (replace Close by Undo) inside the toast to put it back on list and at the same time Dismiss the toast.

My code so far:

archiveItem(item) {
    let toast = Toast.create({
        message: 'Item archived.',
        duration: 2000,
        showCloseButton: true,
        closeButtonText: 'Undo',
        dismissOnPageChange: true,
    });

    var index = this.items.indexOf(item);
    this.items.splice(index, 1); //remove the item


    toast.onDismiss(() => {
        console.log('Dismissed toast');
        this.items.splice(index, 0, item); //put back the item on right place
    });


    this.nav.present(toast);
}

When I click Undo, the item returns to the list, the problem is that if I don't click it, it goes back to the list as well.

I suppose I need to create another function to the Undo, but I don't know how to do that, and Ionic2 DOCS don't talk about it...

Thank you :)

Share Improve this question edited May 26, 2016 at 11:42 sandrina-p asked May 26, 2016 at 10:24 sandrina-psandrina-p 4,1709 gold badges37 silver badges66 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 13

Edit:

Try this:

 toast.onDismiss((data, role) => {    
        console.log('Dismissed toast');
        if (role== "close") {
           this.items.splice(index, 0, item); //put back the item on right place
        }
    });

edit: renamed parameters for clarity

In Ionic 4 this is what worked for me:

    toast.onDidDismiss().then(val => {
      if (val.role== "cancel") {
       //Action
     }
  });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743728614a4497048.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信