Alertcontroller(SOLVED)


#1

Someone knows how to restart a form and return to the home page through the handler button in this method (AlertController)

ex:

this.alertCtrl.create({
title:'Example',
subTitle: 'this is an example',
button:[
  {
 text:'Done! everything look good',
 handler: () => {
    
  }
}
}

#2
handler: () => {
 this.myFormName.reset();
 this.navCtrl.setRoot('HomePage') <--- if lazy loaded
 this.navCtrl.setRoot(HomePage)   <--- if not lazy loaded
 this.navCtrl.pop() <--- if applicable, instead of setRoot()
 }

Or, probably a better option

presentAlert() {

 let myAlert = this.alertCtrl.create({
 title:'Example', 
 subTitle: 'this is an example',
 button:[
  {
 text:'Done! everything look good',
 handler: () => {
    this.nameOfForm.reset();
  }
 })

myAlert.onDidDismiss(() => {
  this.navCtrl.setRoot(HomePage)
  })

myAlert.present();

}

#3

It gives me the following error

image


#4

Maybe post your code so I / we can see the specifics like how you setup your form, where it is, etc


#5
  <form (ngSubmit)="save(forma)" #forma="ngForm">

in my component i only have

user:User = {
    Name: "",
    Email: "",
    Company: "",
    Host: ""
  }

and the last one is the insert function

insert(user:User){
    let url = URL_SERVICES ;
    let body = JSON.stringify({
       Name: user.Name,
       Email: user.Email,
       Company: user.Company,
       Host: user.Host
    });
    let headers = new Headers({
      'Content-Type' : 'application/json'
    });
    return this.http.post(url, body, {headers})
        .map(res=>{
           let data_resp = res.json();


           if(data_resp.error){
             let alerte = this.alertCtrl.create({
               title:'Error',
               subTitle: 'Error! sending the data',
               buttons: ["Accept"]
             });
             alerte.present();
           }else{
            let alerts = this.alertCtrl.create({
              title:'Sucefully',
              subTitle: 'Information Send',
              buttons: [
                 {
                  text: 'Accept',
                  handler: () => {
                    this.forma.reset();
                  }
                }
              ]
            });
            alerts.present();
           }
          })
    
  }

}```

#6

Ah ok. I assumed you were using FormBuilder.
Not sure how to handle clearing the form without using FormBuilder / FormGroup other than manually resetting each value. Not sure how your HTML is set up to get the values. If your fields are just set to

[(ngModel)]="user.Name";
[(ngModel)]="user.Email";
//etc

The only way i know would be simply to reset the values to ""

handler: () => {
 this.user = {
   Name: "",
   Email: "",
   Company: "",
   Host: ""
  }
 }

But I don’t think this approach is applicable to your situation. Sorry.


#7

Create a method in your class called handleDone(). Define your alert handler to be _ => this.handleDone(). Put whatever logic you need into the handleDone method.


#8

@AaronSterling, is there some reasoning behind using handler: _=> as opposed to handler: () => ?
Or is a matter of preference on your end?


#9

The convention is that () is used for a function that returns no arguments,
and _ is used if you want to indicate that you ignore any data returned.
However, _ does not play well with the lodash library, which I don’t use
but a lot of people do. The most correct way is probably to do _ignored =>
instead of either () or _. But I haven’t evolved there yet.


#10

Don’t sweat it. Most programmers still have gills. From reading your posts over time, you’re quite evolved.