anyone know how we used ionic modal object in static methods

14 views
Skip to first unread message

Bhumin Bhandari

unread,
Apr 8, 2018, 7:46:51 AM4/8/18
to ionic professionals

@Injectable()
export class commonLib {
loading: Loading;
constructor(
private loadingCtrl: LoadingController,
private alertCtrl: AlertController,
public modalCtrl: ModalController,
) { } 

static commonAlertPopup(response_message, flag) {
let commonAlert = this.modalCtrl.create(
"CommonAlertPage",
{ alertMsg: response_message, successflag: flag },
{
showBackdrop: false,
enableBackdropDismiss: false,
leaveAnimation: "forward"
}
);
commonAlert.present();
}
}


while I define commonAlertPopup as static type then this.modalCtrl cause error : [ts] Property 'modalCtrl' does not exist on type 'typeof commonLib

Expected :
how we access this.modalCtrl.create in static method?


Mayur Patel

unread,
Apr 8, 2018, 9:25:48 AM4/8/18
to Bhumin Bhandari, ionic professionals
You can not access 'this' inside static methods.
As this commonLib is already Injectable, You can use its instance with Angular's dependency injection and remove static from method signature and make it simple class method.

Example, inside a component, you can inject this commonLib class, like 

import {CommonLib} from 'path to this file'
...
constructor(private commonLib: CommonLib){
}

and use it like 
this.commonLib.commonAlertPopup(....)


Also please change the class name to start with a Capital letter, which is a better convention.

--
You received this message because you are subscribed to the Google Groups "ionic professionals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ionfullapp4pr...@googlegroups.com.
To post to this group, send email to ionfull...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ionfullapp4pro/dd132840-3cb0-46a2-9684-a7f03a683a38%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bhumin Bhandari

unread,
Apr 8, 2018, 9:39:06 AM4/8/18
to Mayur Patel, ionic professionals
Ok, will do that.
--
Bhumin.....
Reply all
Reply to author
Forward
0 new messages