Recently, I encountered an alert within my contractorService.ts:
public showAlertPopup(title: string, message: string, cancelBtnText: string, confirmBtnText: string, onConfirm: any, onCancel: any): void {
console.log("onConfirm function: ", onConfirm);
const alertPopup = this.alertCtrl.create({
title: title,
message: message,
buttons: [
{
text: cancelBtnText,
handler: () => {
onCancel();
}
},
{
text: confirmBtnText,
handler: () => {
onConfirm();
}
}
]
});
alertPopup.present();
}
I realized that I might need to invoke this service from multiple locations. How can I pass the confirm and cancel functions along with parameters?
requestComponent.ts:
public test(): void {
console.log('testing');
}
Another method inside requestComponent.ts:
someOtherFunc(): void {
this.service.showAlertPopup('title', 'message', 'Cancel', 'OK', this.test, null);
}
The above implementation works fine. However, when I attempted:
public test(param: any): void {
console.log('testing: ', param); // the parameter is undefined.
}
someOtherFunc(): void {
this.service.showAlertPopup('title', 'message', 'Cancel', 'OK', this.test.bind('test parameter'), null);
}
I encountered the issue of getting 'testing: undefined' in the output.