question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: ion-button in Alert IonicSafeString events not firing

See original GitHub issue

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x
  • Nightly

Current Behavior

When creating a new alert using AlertController and passing in an IonicSafeString with an <ion-button></ion-button> element, the ion-button rendered in the alert modal does run any click actions or any routerLink directive navigations. The button click event is a no-op.

If ion-buttons cannot be used in this way, then the documentation is incorrect here:

import { IonicSafeString, ToastController } from '@ionic/angular';

...

constructor(private toastController: ToastController) {}

async presentToast() {
  const toast = await this.toastController.create({
      message: new IonicSafeString('<ion-button>Hello!</ion-button>'),
      duration: 2000
  });
  toast.present();
}

Expected Behavior

When you want to create an alert with additional ion-button elements with routerLink navigations within the alert modal (as opposed to in the buttons array), this should be enabled when using an IonicSafeString

Steps to Reproduce

Create an alert as follows and see that the ion-button element will not route to the desired path (or handle any click event).

    private readonly alertCtrl: AlertController
    ...
    const alert = await this.alertCtrl.create({
      header: 'Title',
      message: new IonicSafeString(`Some other details, click here:
        <ion-button routerLink="desired/path/">Button</ion-button>`),
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
        },
      ],
    })
    await alert.present()

Code Reproduction URL

No response

Ionic Info


Ionic:

   Ionic CLI: 6.20.1
   Ionic Framework: @ionic/angular 6.0.13
   @angular-devkit/build-angular : 13.3.0 
   @angular-devkit/schematics : 13.3.0 
   @angular/cli : 13.3.0 
   @ionic/angular-toolkit : not installed

Utility:

   cordova-res : not installed globally
   native-run  : not installed globally

System:

   NodeJS : v16.11.0
   npm : 8.0.0
   OS : macOS Catalina

### Additional Information

_No response_

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
liamdebeasicommented, Jul 13, 2022

If you eject from the sanitizer, Ionic will not strip out click handlers. You are still using the raw Web Component, so you will need to use an onclick handler or use addEventListener instead of (click) in Angular.

1reaction
elvececommented, Jul 11, 2022

Thanks for the issue. Can you provide a GitHub repo I can use to verify this behavior?

I don’t have this committed in a project as I ended up writing a custom component. I could spin up a demo repo if that is necessary?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ion-button Click not firing up function in ionic 4 - Stack Overflow
I've been toying with this for an hour trying to figure out what simple click events don't work in Ionic. ( I too,...
Read more >
ion-alert: Ionic API Alert Buttons with Custom Message Prompts
ion-alert dialog presents or collects information using inputs. Custom alert button messages appear above the app's content and must be manually dismissed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found