Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters in pop method of ionic2

I tried passing parameters in push method of ionic2. like this

this.nav.push(SecondPage, {
    thing1: data1,
    thing2: data2
});

but is there any way to pass parameter in pop().

like image 525
Akash Rao Avatar asked Mar 30 '16 14:03

Akash Rao


3 Answers

This is how I achieved it in ionic-3 and find it easier.

Page from where we pop()

 this.navCtrl.getPrevious().data.thing1 =data1;
 this.navCtrl.getPrevious().data.thing2 =data2;
 this.navCtrl.pop();

Page after pop():

public ionViewWillEnter() {
     this.thing1 = this.navParams.get('thing1')|| null;
     this.thing2 = this.navParams.get('thing2')|| null;
} 
like image 192
Prashant Avatar answered Nov 14 '22 00:11

Prashant


I suggest you use Events. All you have to do is to subscribe to an event on the parent page and then publish the event on the child passing the data you want:

// taken from the docs
import { Events } from 'ionic-angular';

constructor(public events: Events) {}

// first page (publish an event when a user is created)
function createUser(user) {
  console.log('User created!')
  events.publish('user:created', user);
}

// second page (listen for the user created event)
events.subscribe('user:created', (userEventData) => {
  // userEventData is an array of parameters, so grab our first and only arg
  console.log('Welcome', userEventData[0]);
});
like image 14
Lucas Moulin Avatar answered Nov 14 '22 00:11

Lucas Moulin


Currently, I believe that there is no way of accomplishing this.

There is a Github issue for it though, that has got some great discussion on it by the Ionic core team. It sounds like they have added it to the Ionic 2 roadmap, too! The Github issue also has some proposed work-arounds, such as adding the ParentPage to the NavParams going to the ChildPage, but it is all quite a bit hacky.

like image 2
TheBrockEllis Avatar answered Nov 14 '22 01:11

TheBrockEllis