18

I am unable to make it work. The popup calling code is

presentPopover(myEvent){
        //alert('invoke popup')
      let popover = this.popoverCtrl.create(PopoverPage, this.data);
      popover.present(
          {
            ev: myEvent
          }
      );
}

and the part where i need to access currently is:

export class PopoverPage{

  constructor(public viewCtrl: ViewController) {
   // alert('in the popup')  
   //alert(this.data)
  }

So how will the data be avaialbe in the popupover page component ?

2 Answers 2

46

Parameters/data can be passed to the Popover like this

let popover = this.popoverCtrl.create(PopoverPage, {key1:value1, key2: value2});

And then you can use NavParams to retrieve the data passed to the Popover.

    export class PopoverPage{        
      constructor(public navParams:NavParams) {
       console.log(this.navParams.data);
       let value1 = this.navParams.get('key1');
       let value2 = this.navParams.get('key2');
      }
    }

For ionic v4, you can send data using componentProps to pass data and retrieve through navParams.

const popover = await this.popoverController.create({
      component: PopoverPage,
      componentProps:{key1:value1, key2: value2}
    });
    return await popover.present()
4
  • Also, you can get data in popover component and via View Controller, which is a more correct way: import { ViewController } from 'ionic-angular'; export class PopoverComponent { constructor( public viewCtrl: ViewController ) {} // here we are getting data object which we set in parent component public data: any= this.viewCtrl.data; } Commented May 24, 2018 at 14:04
  • @nikola.maksimovic thats interesting... I will have to check and update.. The doc doesnt seem to show that property..
    – Suraj Rao
    Commented May 24, 2018 at 14:21
  • 1
    what is method in ionic 4. I got undefined in ionic 4 for this method
    – sainu
    Commented Apr 5, 2019 at 5:49
  • Ya I got it. In ionic 4 there is a property called componentProps in PopoverController Description:The data to pass to the popover component. Type :{ [key: string]: any; }. And we can retrieve it as same as ionic 3, means as NavParams @SurajRao
    – sainu
    Commented Apr 5, 2019 at 7:40
7

You have to pass this.data as an json object, after then you can access the value with key.

Calling popup code:

presentPopover(myEvent){
    //alert('invoke popup')
  this.data = {data_key:'your_value'};
  let popover = this.popoverCtrl.create(PopoverPage, this.data);
  popover.present(
      {
        ev: myEvent
      }
  );
}

Accessing value from popover:

export class PopoverPage{
  constructor(public viewCtrl: ViewController,public navParams:NavParams) {
     // alert('in the popup');
     //alert(this.navParams.get('data_key'));
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.