我使用 ionic2(+ angular2 使用 javascript,而不是 typescript)创建了一个 toast,用户从列表中删除了一个项目。
到目前为止一切顺利,现在我想在 toast 中添加一个按钮 Undo
(将 Close 替换为 Undo)以将其放回列表中,同时关闭 toast。
到目前为止我的代码:
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);
}
当我单击撤消
时,该项目返回到列表,问题是如果我不单击它,它也会返回到列表。
我想我需要为 Undo
创建另一个函数,但我不知道该怎么做,而且 Ionic2 DOCS 也没有谈论它...
谢谢你:)
最佳答案
编辑:
试试这个:
toast.onDismiss((data, role) => {
console.log('Dismissed toast');
if (role== "close") {
this.items.splice(index, 0, item); //put back the item on right place
}
});
编辑:为清楚起见重命名参数
关于javascript - ionic2 - 仅当用户在 Toast 中单击 "close"时才创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37458092/