Is it possible to make global service for Alert?

I tried

import { Injectable } from '@angular/core';
import { NavController, Alert, App } from 'ionic-angular';

@Injectable()
export class MyAlert {
    private nav: NavController;
    constructor(private app: App) {
        this.nav = app.getActiveNav();
    }

    myAlert(title: string, text: string) {
        let alert = Alert.create({
            title: title,
            subTitle: text,
            buttons: ['OK']
        });
        this.nav.present(alert);
    }
}

On pages it throws exception:

Can't resolve all parameters for NavController: (?, ?, ?, ?, ?, ?, ?, ?).

If I’m not wrong, you should declare the controller inside the constructor:

constructor(private app: App, private nav: NavController) {
    this.nav = app.getActiveNav();
}

Unfortunately the same effect

Using Ionic 2 RC4:

import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';

@Injectable()
export class ServiceAlert {

  constructor(public alertCtrl: AlertController) {}

  myAlertMethod(title: string, message: string) {
    let confirm = this.alertCtrl.create({
      title: title,
      message: message,
      enableBackdropDismiss: false,
      buttons: [
        {
          text: "OK",
          handler: () => {
            console.log('OK clicked');
          }
        }
      ]
    });
    confirm.present();
  }
}
1 Like

How would you add methods for the handler?

1 Like
import { Injectable } from '@angular/core';
import { AlertController } from 'ionic-angular';

@Injectable()
export class ServiceAlert {

  constructor(public alertCtrl: AlertController) {}

  myAlertMethod(title: string, message: string, handler: any) {
    let confirm = this.alertCtrl.create({
      title: title,
      message: message,
      enableBackdropDismiss: false,
      buttons: [
        {
          text: "OK",
          handler: handler
        }
      ]
    });
    confirm.present();
  }
}
1 Like

How will I return the values to the calling controller ?

Example - i created a service and injected in the controller. Now I want the handler in the calling component.

Any idea?

can you just pass the handler to the function you are calling to present like above

?