×

Ionic Alert

Ionic Alert

An Alert is a dialog box that presents the users with important information, or it stores the data from the user by using the inputs. Alerts are a great way to provide the user with the ability to choose a particular action or list of actions. It appears at the top of the application content. The user must manually ignore them before they can resume interaction with the application. It may also include a header, subheader, and message as an alternative.

An Alert can be thought of as a floating modal, which covers only a part of the screen. This means that an alert should be used for quick actions such as passwords, verifications, small app notifications, or many other options. Alerts are extremely flexible and can be customized easily.

Ionic Alert Controller

Alert controller basically used in the ionic application, which controls the alert component. You can create and dismiss the alerts by using the alert controller. Alerts are very flexible, and it can be customized easily. There are different types of alerts in an ionic application which are shown below:

  • Basic Alerts
  • Confirmation Alerts
  • Radio Alerts
  • Prompt Alerts
  • Checkbox Alerts

Basic Alerts

It is used to notify the user about the new information. This information can be about a change in the application, a new feature, and an urgent situation that requires acknowledgment. It also notifies the user about the confirmation of the action was successful or not.

Example: The below example shows how you can use the basic alerts in the ionic application. It shows many alert options in the app such as headermessagealerts, etc.

Home.page.html

 
   
  
  Ionic Alert Example
   
   
  
  
  
   

Home.page.ts

import { Component } from '@angular/core'; 
 import { AlertController } from '@ionic/angular'; 
 @Component({ 
  selector: 'app-home', 
  templateUrl: 'home.page.html', 
  styleUrls: ['home.page.scss'], 
 }) 
 export class HomePage { 
  constructor(public alertCtrl: AlertController) { }  
 async showAlert() { 
  const alert = await this.alertCtrl.create({ 
  header: 'Alert', 
  subHeader: 'Battery Alert', 
  message: 'Your battery about to die', 
  buttons: ['OK'] 
  }); 
  await alert.present(); 
  const result = await alert.onDidDismiss();  
  console.log(result); 
  } 
 }  

Output: After executing this code, you will get the output, as shown below:

After executing this code

When you click on the basic alert button, then it shows the alert message. If you click on the OK button, then the alert message will be disappeared.

 alert message will be disappeared.

Confirmation Alerts

Confirmation Alerts are used when the user has to confirm a specific choice explicitly before moving forward in the app. The common example of the Confirmation Alert is checking to ensure that a user wants to create or delete a message from their chatbox.

Example: The below example shows how you can use the confirmation alert in the ionic application.

Home.page.html

 
   
  Confirmation Alert 
   
  
  
  
Alert

Home.page.ts

import { Component } from '@angular/core'; 
 import { AlertController } from '@ionic/angular'; 
 @Component({ 
  selector: 'app-home', 
  templateUrl: 'home.page.html', 
  styleUrls: ['home.page.scss'], 
 }) 
 export class HomePage {  
  constructor(public alertCtrl: AlertController) { } 
  async showConfirm() { 
  const confirm = await this.alertCtrl.create({ 
  header: 'Confirmation', 
  message: 'Do you want to exit this game', 
  buttons: [
  { 
  text: 'Ok',
  role: 'Ok', 
  handler: () => {  
  console.log('Confirm Ok'); 
  } 
  }, 
  { 
  text: 'Cancel', 
  role: 'Cancel',
  handler: () => { 
  console.log('Confirm Cancel.');  
  } 
  } 
  ] 
  }); 
  await confirm.present(); 
  } 
 }  

Output: After executing this code, you will get the output, as shown below:

Confirmation Alerts

When you click on the Alert button, then it shows the confirmation alert message. If you click on the OK and Cancel button, then the alert message will be disappeared

Radio Alerts

Radio Alerts are similar to the confirmation alerts, but you can use the radio component to provide a number of options. It provides a set of options to the users, but you can select only one option.

Example: The below example shows how you can use the radio alert in the ionic application.

Home.page.html

 
   
  Radio Alert 
   
  
  
  
Show Radio Alert

Home.page.ts

import { Component } from '@angular/core'; 
 import { AlertController } from '@ionic/angular'; 
 @Component({ 
  selector: 'app-home', 
  templateUrl: 'home.page.html', 
  styleUrls: ['home.page.scss'], 
 }) 
 export class HomePage { 
  constructor(public alertCtrl: AlertController) { } 
  async showRadio() {  
  const alert = await this.alertCtrl.create({ 
  inputs: [ 
  { 
  name: 'radio 1', 
  type: 'radio', 
  label: 'Java', 
  value: 'Java', 
  }, 
  { 
  name: 'radio 2', 
  type: 'radio', 
  label: 'Python', 
  value: 'Python', 
  checked: true,
  }, 
  { 
  name: 'radio 3', 
  type: 'radio', 
  label: 'Android', 
  value: 'Android', 
  }, 
  { 
  name: 'radio 3', 
  type: 'radio', 
  label: 'AngularJs', 
  value: 'AngularJs', 
  }, 
  ],
  buttons: [ 
  { 
  text: 'Save', 
  handler: data => {  
  console.log('Save clicked'); 
  } 
  }, 
  { 
  text: 'Cancel', 
  handler: data => { 
  console.log('Cancel clicked');  
  } 
  }, 
  ] 
  }); 
  await alert.present(); 
  } 
 } 

Output: After executing this code, you will get the output, as shown below:

Radio Alerts

When you click on the Show Radio Alert button, then it shows the Radio alert message.

Radio alert message

Prompt Alerts

Prompt alerts provide a way for the user to input data or information. Sometimes, Prompt alerts are used to ask the password from the user before progressing forward in the application.

Example: The below example shows how you can use the prompt alert in the ionic application.

Home.page.html

 
   
  Ionic Alert 
   
  
  
  
Show Prompt Alert

Home.page.ts

import { Component } from '@angular/core'; 
 import { AlertController } from '@ionic/angular'; 
 @Component({ 
  selector: 'app-home', 
  templateUrl: 'home.page.html', 
  styleUrls: ['home.page.scss'], 
 }) 
 export class HomePage { 
  constructor(public alertCtrl: AlertController) { } 
  async showPrompt() {  
  const prompt = await this.alertCtrl.create({ 
  header: 'Courses', 
  message: 'Enter the Course name', 
  inputs: [ 
  { 
  name: 'title', 
  type: 'text', 
  placeholder: 'Course name' 
  }, 
  { 
  name: 'title', 
  type: 'text', 
  placeholder: 'Course name' 
  }, 
  ], 
  buttons: [  
  { 
  text: 'Save', 
  handler: data => { 
  console.log('Save clicked'); 
  } 
  }, 
  { 
  text: 'Cancel',  
  handler: data => { 
  console.log('Cancel clicked'); 
  } 
  } 
  ] 
  }); 
  await prompt.present(); 
  } 
 }  

Output: After executing this code, you will get the output, as shown below:

Prompt Alerts

When you click on the Show Prompt Alert button, then it shows the Prompt alert. Here, you can input the value in the placeholder.

Show Prompt Alert button

Checkbox Alerts

Checkbox alerts are similar to the confirmation alerts, but you can use the checkbox component to provide the multiple choices. It offers the user a set of multiple choices for the selection.

Example: The below example shows how you can use the checkbox alert in the ionic application.

Home.page.html

 
   
  CheckBox Alert 
   
  
  
   
  

Home.page.ts

import { Component } from '@angular/core'; 
 import { AlertController } from '@ionic/angular'; 
 @Component({ 
  selector: 'app-home', 
  templateUrl: 'home.page.html', 
  styleUrls: ['home.page.scss'], 
 })  
 export class HomePage { 
  constructor(public alertCtrl: AlertController) { } 
  async showCheckBoxAlert() { 
  const alert = await this.alertCtrl.create({ 
  header: 'Games', 
  message: 'Select your favourite game',
  inputs: [ 
  { 
  name: 'checkbox', 
  type: 'checkbox', 
  label: 'Hockey', 
  value: 'Hockey', 
  }, 
  { 
  name: 'checkbox', 
  type: 'checkbox', 
  label: 'Cricket',  
  value: 'Cricket',
  checked: true, 
  }, 
  { 
  name: 'checkbox', 
  type: 'checkbox', 
  label: 'Badminton', 
  value: 'Badminton',  
  }, 
  ], 
  buttons: [ 
  { 
  text: 'Save', 
  handler: data => { 
  console.log('Save clicked'); 
  } 
  }, 
  { 
  text: 'Cancel', 
  handler: data => { 
  console.log('Cancel clicked'); 
  } 
  } 
  ] 
  });  
  await alert.present(); 
  } 
 } 

Output: After executing this code, you will get the output, as shown below:

Checkbox Alerts

When you click on the Show Checkbox Alert button, then it shows the Checkbox alert. Here, you can select the value.

Show Checkbox Alert button

Related Topics

Ionic Navigation and Routing

The latest version (ionic 4) of the ionic framework, no longer limited to the angular. In Ionic 4, you can create an ionic project in different front-end frameworks such as Vue.js, React.js, and...

6 minutes read.

Ionic Contents

Ionic Contents The content component provides an easy-to-use content area with some useful ways to control the scrollable region. There should be only one content in one view. Content, along with several other Ionic...

2 minutes read.

Ionic Actionsheet

Ionic Actionsheet The Action sheet is a dialog box that displays a group of options. It is shown on the top of the application content and must be dismissed manually before the user can resume...

3 minutes read.

Ionic Menu

The Ionic menu is the side-menu navigation that can be slide in from the side of the current view. By default, the menu slides in from the left side, but the side...

7 minutes read.

Ionic Slides

The ionic slides component is the multi-section container. You can swipe or drag any segment between them. This contains any number of slides components. Slides help it easy to make galleries, tutorials, and...

2 minutes read.

Ionic Editors

Editors are software programs that permit the users to create and edit the text files. In the development field, editors are usually used to create the source code editors that includes many specific...

3 minutes read.

Ionic vs Apache Cordova

Ionic Vs Apache Cordova Ionic and Cordova are two popular technology going on in the market for mobile application development. The combination of these two technologies creates a development platform that is known as...

3 minutes read.

Ionic Popover

The Ionic popover is a small dialog box that displays at the top of the screen. It can be used for anything, but it is usually used for a button, an action,...

4 minutes read.

Ionic Splash Screen

The Apache Cordova plugin helps to displays and hides a splash screen during the application launch. The Ionic Splash Screen is similar to a home page on a website. The ionic splash...

2 minutes read.

Ionic Chip

Ionic chip displays the complex entities in small entities in small blocks, such as the contacts. Ionic chip has several types of elements like avatars, thumbnails, text, and icons. It is a bubble-like...

3 minutes read.

Ionic Buttons

Ionic Buttons There are different types of buttons in the Ionic framework. These buttons are subtly animated, enhancing the user experience when using the application. Buttons are a fundamental way of interacting and navigating...

4 minutes read.

Ionic-fab Button

Ionic-fab Button The Ionic FABs (Floating Action Buttons) are standard material design compounds. It is a container element that contains one or more fab buttons. They are required to be placed in a fixed position,...

2 minutes read.

Ionic vs PhoneGap

Ionic Vs PhoneGap Ionic and PhoneGap are both used to develop the mobile application. Both Frameworks are used in large enterprises by the developers. Here, we are going to compare the important differences between ionic...

3 minutes read.

Ionic framework Installation

Installation of Ionic Before proceeding with the ionic application, you'll need to install node.js and NPM (Node Package Manager), the code editor, and Ionic CLI. Installation of node.js and NPM These are the primary...

5 minutes read.

Ionic Checkbox

Ionic Checkbox Ionic Checkbox is nearly the same as a toggle. It can be placed in an ionic element or used as a separate checkbox. It is designed differently for every platform, such...

2 minutes read.

Ionic Date Time

Ionic Date Time The DateTime component is used to display an interface that makes it simple for users to select dates and times. Tapping on <ion-datetime> will display a slider up from the...

4 minutes read.

Ionic Toggle

The toggle is the same as the HTML checkbox entry, but it looks different and easier to use on a touchscreen device. It is an input component that holds the boolean value. Toggle...

2 minutes read.

Ionic Tutorial for Beginners

What is Ionic? Ionic is an open-source framework that is used for the development of mobile applications. It is also used to build android applications, iOS, applications, desktop applications, and web applications using web...

4 minutes read.

Ionic List

Ionic List The ionic list is created of several rows of items that can contain text, buttons, thumbnails, keys, icons, and more. Lists are one of the essential components in any web or...

4 minutes read.

Ionic Tabs

The ionic tabs mostly used for mobile navigation. It means that tabs are placed at the top of the display on android devices, while at the bottom on IOS. It does not provide...

2 minutes read.