ionic
  1. ionic-alert

Ionic Alert

Ionic Alert is a UI component that displays a message or notification to the user, forcing them to interact with it and acknowledge its content.

Syntax

<ion-alert
  header="Alert Header"
  subHeader="Alert Subheader"
  message="Alert Message"
  buttons=["ButtonText":"ButtonHandler"]>
</ion-alert>

Example

<ion-alert
  header="Confirmation Required"
  message="Are you sure you want to delete this item?"
  buttons=[{"text":"Cancel", "role":"cancel"}, {"text":"Delete", "handler":deleteItem}]>
</ion-alert>

Output

When the above code is run, it displays a confirmation alert with the message "Are you sure you want to delete this item?". It also has two buttons - "Cancel" and "Delete".

Explanation

  • header: A title for the alert.
  • subHeader: A subtitle for the alert.
  • message: The main contents of the alert.
  • buttons: An array of buttons to display below the message. Each button is an object with two properties - text (the text of the button) and handler (the function to call when the button is clicked). There is also an optional role property that can be set to "cancel" for the cancel button.

Use

Ionic Alert can be used to display messages or notifications to the user in a pop-up format. It is often used for confirmation prompts or error messages.

Important Points

  • Ionic Alert requires @ionic/angular to be installed.
  • The ion-alert tag must be nested within ion-content.
  • The buttons array must contain at least one button.
  • The handler function must be defined in the component's TS file and passed as an argument to the button object.

Summary

Ionic Alert is a UI component that displays a pop-up message or notification to the user. It has a simple syntax and can be customized with a header, subheader, message, and buttons. It is used to prompt the user for confirmation or display error messages.

Published on: