Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2, how to properly use alert

Tags:

ionic2

I am programming using Ionic 2, and I want to show a pop-up alert when a button is clicked.

This is the code in my home.html:

<button (click)='openFilters()'>CLICK</button>

And in my home.ts

import {Component} from '@angular/core';
import {Page, NavController, Alert} from 'ionic-angular';

@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
  constructor(nav: NavController, alertCtrl: AlertController) {

  }

  openFilters() {
    let alert = this.alertCtrl.create({
        title: 'Low battery',
        subTitle: '10% of battery remaining',
        buttons: ['Dismiss']
    });
    alert.present();
  }
}

I have read some StackOverflow questions on this topic and tried to implement it like this:

openFilters() {
    let alert:Alert = Alert.create({
        title: 'Low battery',
        subTitle: '10% of battery remaining',
        buttons: ['Dismiss']
    });
    this.nav.present(alert);
  }

Still, I accomplished nothing; I get errors.

like image 349
yudhie.z.kurniawan Avatar asked Dec 19 '22 14:12

yudhie.z.kurniawan


1 Answers

Make sure to import this:

import {AlertController} from 'ionic-angular';

and have code such as this:

constructor(private alertController: AlertController) {

}


openFilters() {
    let alert = this.alertController.create({
        title: 'Example',
        subTitle: 'Example subtitle',
        buttons: ['OK']
    });

    alert.present();
}

Things have changed with Beta 11, and it seems as if the documentation online is not yet updated, you can always go into the ionic-angular folder in your node_modules and find the component that you are trying to use for examples of better documentation.

like image 66
Jay Ordway Avatar answered Mar 20 '23 08:03

Jay Ordway