Home:ALL Converter>ionic2 AlertController scope issues

ionic2 AlertController scope issues

Ask Time:2016-11-07T13:44:59         Author:vishnu

Json Formatter

I am using ionic2/angular2 application for developing mobile application. we have to call another external API to show one dialog box. After returning from another control to my application the AlertController scope is not working.

    import {AlertController, Events } from 'ionic-angular';
    export class BalanceInquiryPage {
      constructor(){
        public alertCtrl: AlertController,
        public events: Events,
      }

      public balanceDetials(formdata): void {
          //fetch the device details from sqlite storage
          this.databaseService.getDeviceDetails().then((result) => {
             this.commonServices.getGeoLocation(this.deviceObj);
             setTimeout(() => {
               let geodevice = this.shareService.getDeviceDetails();
               let balanceenq = JSON.stringify({"mobileNumber":"9199403562", "expDate":"","cardDigits":""});         
              if (window.npciLib){
                 window.npciLib.balanceenq(balanceenq, function (data) {                  
                      let dataobj = JSON.parse(data);
                      if(dataobj.resp.result == 'SUCCESS'){
                        //this.events.publish('alert:presented', 'Success', 'Balance Enquiry Success.', MyaccountPage);
                        let alert = this.alertCtrl.create({
                          title: "Success",
                          subTitle: "balance",
                          buttons: [
                            {
                              text: 'Ok',
                              handler: () => {

                              }
                            }
                          ]
                        });
                        alert.present();
                      }
                      else{
                        //this.events.publish('alert:presented', 'Fail', 'There was an error in balance enquiry.', 'samePage');
                      }
                   }, function (error) {
                      console.log('error block-'+error);
                   });
               }
              }, 1000);
         }, (error) => {
           console.log('Error', error.err);
         });
        }
   }

I am getting the following error :

Uncaught TypeError: Cannot read property 'alertCtrl' of null

Author:vishnu,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40458544/ionic2-alertcontroller-scope-issues
yurzui :

Use arrow function here\n\nwindow.npciLib.balanceenq(balanceenq, function (data) { \n\n\nlike\n\nwindow.npciLib.balanceenq(balanceenq, (data) => { \n\n\nThis way this will be referenced to your component instance",
2016-11-07T05:48:42
yy