Call AlertController function

From documentation

  async presentAlert() {
    const alert = await this.alertController.create({
      header: 'Alert',
      subHeader: 'Subtitle',
      message: 'This is an alert message.',
      buttons: ['OK']
    });

    await alert.present();
  }

this function generates an alert message and it is called after clicking a button. Is there any other way to call the function, just like you call a regular function, for the alert message to be displayed? Like presentAlert();

Take my example, change the values…

async verificarCarrinhoVazio() {
      let confirm = await this.alertCtrl.create({
        header: 'Carrinho Vazio',
        message: 'Seu carrinho está vazio. <br> <br> Adicione mais produtos e finalize seu pedido!',
        backdropDismiss: false,
        buttons: [
          {
            text: 'Comprar',
            handler: () => {
              // Here for to go!
              this.router.navigateByUrl('/home');
            }
          },
          {
            text: 'Cancelar',
            handler: () => {
            }
          }
        ]
      });
      confirm.present();
  }
1 Like

That handler is pretty good. For some reason this.router.navigatebyUrl does not do anything. I get rid of the modal page after clicking the button of the alert dialog box with this.closeModal()

closeCheckout() {
    this.modalCtrl.dismiss();
  }

In my app.routing.module.ts the home path is :

const routes: Routes = [
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full'
  },
  {
    path: 'home',
    loadChildren: () => import('./home/home.module').then(m => m.HomePageModule)
  },

No matter if I change the path

this.router.navigateByUrl('/home');
this.router.navigateByUrl('./home/home.module');
this.router.navigateByUrl('./src/home/home.module);

won’t link to the home page.

I’m sorry this router is for Ionic 4…

In Ionic 3 use the navigateController

I do work on ionic 4, this is what ionic info prints

> Ionic:
> 
>    Ionic CLI                     : 5.2.5 
>    Ionic Framework               : @ionic/angular 4.8.0
>    @angular-devkit/build-angular : 0.801.3
>    @angular-devkit/schematics    : 8.1.3
>    @angular/cli                  : 8.1.3
>    @ionic/angular-toolkit        : 2.0.0

Do you import: → import { Router } from '@angular/router';

If dosen’t work change the:
this.router.navigateByUrl('/home');
for
this.router.navigate(['/home']);