简体   繁体   中英

ionic2 AlertController, how to return the buttons clicked value from method

I m fairly new in Typescript , I have following code in provider

...
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();
    }

UPDATE:

There is a button in a page, which calls a function deleteItems

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

}

I want the confirm method to return true or false depending upon the button "Yes" or "No" clicked.

Can anybody tell me how to achieve this?

Both buttons take a handler each. I would use the handlers to set a .

Define arrow functions

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

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

let confirm function take the handlers along with message:

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();
    }

call the confirm function:

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

You can do it as shown below.

Note: Creating an UI related things with Provider is treated as Anti-Pattern on Ionic2.Because provider is to handle service related use cases.Keep that in mind too.

Would you like to see the alternative? Please see 2nd Method of my answer here . In other words you can use a base class for handling it.

 isTrue:boolean=null;

    constructor(){}

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

            confirm.present();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM