Pass data using viewCtrl.dismiss();

Hi, how do i pass json or object data or multiple data using this.viewCtrl.dismiss();

here is the code snippet:
dismiss() {
this.viewCtrl.dismiss();
}

chooseItem(item: any) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({‘address’: item}, function(results, status) {
console.log(results);
if (status === ‘OK’) {
this.lat = results[0].geometry.location.lat();
this.long = results[0].geometry.location.lng();
console.log(this.lat + " " + this.long);

      this.viewCtrl.dismiss(item, this.long, this.lat); // here i want to pass this datas, it will display the error " cannot read property of dismiss undefined"
      
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });

}

put it into an array

@Chungtheng what exactly and how is it done?

presentModal() {
let modal = this.modalCtrl.create(ListPgn,{data:this.issData});
modal.onDidDismiss(data=>{
console.log(data) // this data is the data when you dismiss
})
modal.present();
}

dismiss() {
var data:any =[]
data={
“long” : this.long,
“lat” : this.lat
}
this.viewCtrl.dismiss(data);
}

@Chungtheng it errors in this line

this code is inside the chooseItem function, inside it theres this geocoder, and a function function inside it
this is where i call the dismiss because when i put in outside the function

the long lat with not be stored and will display ‘undefine’

dismiss() {
this.viewCtrl.dismiss();
}

chooseItem(item: any) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({‘address’: item}, function(results, status) {
console.log(results);
if (status === ‘OK’) {
this.lat = results[0].geometry.location.lat();
this.long = results[0].geometry.location.lng();
console.log(this.lat + " " + this.long);

    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });

this.data_pass = {
        place: item,
        lat: this.lat, //undefine
        long: this.long //undefine
      }
       console.log(this.data_pass);
      this.viewCtrl.dismiss(this.data_pass);

}

so you got undefine ?

Your code can’t fine viewCtrl too

yes if i put the dismiss outside the function, if i put it inside it error, is says the property dismiss is undefined too

try

let _me = this
_me.viewCtrl.dismiss(this.data_pass)

data = {thing1: stuff, thing2: stuff, thing3: stuff};
this.viewCtrl.dismiss(data);

chooseItem(item: any) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({‘address’: item}, function(results, status) {
console.log(results);
if (status === ‘OK’) {
this.lat = results[0].geometry.location.lat();
this.long = results[0].geometry.location.lng();
// console.log(this.lat + " " + this.long);
this.data_pass = {
place: item,
lat: this.lat,
long: this.long
}
console.log(this.data_pass); //data is still here

    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });

console.log(this.data_pass); // data is lost
this.viewCtrl.dismiss(this.data_pass);

}

this is the problem @Chungtheng

chooseItem(item: any) {
let _me = this

}

try to replace “this” keywork with “_me”

1 Like

@Chungtheng thank you very much for the help, this worked and it helps a lot ^^ thank you!! Godbless! ^^

1 Like

You welcome, try to remember , callback function not work with keywork “this”

Yes sir :slight_smile: ill keep that in mind, thank you for the new knowledge.

1 Like

hi, can i ask one thing, i have 2 modal. modal 1 consist of ori datas. modal 2 is where i changed stuff.
however when i use this.viewCtrl.dismiss(); on modal 2, the changed datas is carried back to modal 1. do u have any idea why?