angular - ionic2 AlertController,如何从方法返回按钮单击的值

标签 angular typescript ionic-framework ionic2

我对 Typescript 相当陌生,我在提供程序中有以下代码

...
confirm(message, title=""){
        let confirm = this.alertCtrl.create({
            title: title || "Please confirm",
            message: message,
            buttons: [
                {
                  text: 'Yes',
                  handler: () => {
                    return true;
                  }
                },
                {
                  text: 'No',
                  handler: () => {
                    return false;
                  }
                }
              ]
        });

        confirm.present();
    }

更新:

页面中有一个按钮,它调用函数deleteItems

deleteItems(){
    this.popup.confirm("Are you sure you want to delete this item");
    if(this.popup.isTrue == true){
          //delete items
     }

}

我希望 confirm 方法根据单击的按钮"is"或“否”返回 true 或 false。

谁能告诉我如何实现这一目标?

最佳答案

两个按钮各有一个处理程序。我将使用处理程序来设置a

定义箭头函数

a:boolean;
onYesHandler = ()=>{
   this.a = true;
}

onNoHandler = () =>{
   this.a = false;
}

confirm函数获取处理程序和消息:

confirm(message, yesHandler,noHandler,title=""){
        let confirm = this.alertCtrl.create({
            title: title || "Please confirm",
            message: message,
            buttons: [
                {
                  text: 'Yes',
                  handler: yesHandler
                },
                {
                  text: 'No',
                  handler: noHandler
                }
              ]
        });

        confirm.present();
    }

调用confirm函数:

this.popup.confirm("Are you sure you want to delete this item",this.onYesHandler,this.onNoHandler);

关于angular - ionic2 AlertController,如何从方法返回按钮单击的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165285/

相关文章:

javascript - 另一个 javascript 库上的 Angular 2 templateURL 功能

Angular 2 数据绑定(bind)和 setTimeout

javascript - 我可以在加载页面之前将数据保存在 guard 中吗

javascript - Angular 4 禁用的 FormControl 不会检查或显示验证错误

angular - 运行 ng e2e 时 dist/out-tsc-e2e 在哪里?

Angular 4 与 Material

javascript - Nightmare.js 屏幕截图缓冲区长度为 0

javascript - ionic 平台 ng-show 只是第二次工作

css - ionic : stretch image in card to device width

ios - 为 iOS 构建后 Ionic 应用程序链接器错误