×

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 header, message, alerts, 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 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 Segment

A segment is a collection of buttons, sometimes which are known as segmented controls. The segment buttons are displayed at horizontal rows. These segment buttons can be shown in a toolbar or the...

2 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 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 Refresher

Refresher means updating the currently showing page so that the user can see the latest view. It is also known as re-loading in web technology. It provides a pull- to-refresh feature on a...

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 Range

The Range slider lets users choose from a range of values by moving the slider knob. You can accept two knobs, but by default, one-knob controls the range value. Range labels You can put down on...

2 minutes read.

Ionic Modal

Ionic modal is displayed as a temporary UI that slides into the screen. The ionic modal is a dialog box that is displayed at the top of the app’s content, and it...

5 minutes read.

Ionic Spinner

The ionic spinner component provides the various SVG (Scalable Vector Graphic) spinners. Ionic spinners are the visual indicators that display the app which is loading content or performing another process that the user...

5 minutes read.

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...

5 minutes read.

Ionic Camera

Hybrid mobile applications can make very quickly, but sometimes it becomes a challenge to get the features that access the hardware such as GPS, storage, file storage, cameras, and networks. Cordova offers plugins...

4 minutes read.

Ionic Toolbar

A toolbar is a general toolbar that can be used in an application such as a header, sub-header, footer, or even a sub-footer. Because the ion-toolbar is flexbox-dependent, no matter how many toolbars...

3 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 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 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 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 Grid

The Ionic grid is a very powerful mobile-first flexbox system for building the custom layouts. It is a CSS feature supported by all devices that ionic supports. The Ionic grid is composed of...

11 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 Reorder

The Ionic offers the <ion-reorder> component to reordering features. Ionic reorder component adds the Drag and Drop feature to list items, in which the user can change the order of the items. Reorder...

3 minutes read.

Ionic vs React Native

Ionic Vs React Native Ionic and React Native are both common framework which is used to develop mobile applications. Here, you will know about the popular differences between the ionic and the React Native. Ionic Ionic...

3 minutes read.