1

I've got 3 routes. When I move from 1st to 2nd I need to pass a parameter: this.navCtrl.push(ReadyPage, { newGame: true });

But I also need to pass sometimes this parameter as well when I moving from 3rd to 2nd.

I'm trying with this but doesn't work: this.viewCtrl.dismiss({ newGame: true });

Comparing both methods dismiss doesn't seem to have that option (unless it's called data instead params): abstract push(page: Page | string, params?: any, opts?: NavOptions, done?: TransitionDoneFn): Promise<any>; dismiss(data?: any, role?: string, navOptions?: NavOptions): Promise<any>;

2
  • Can you explain your problem with more details, please? What type of page you have there? In which case you want to pass what params? Commented Feb 6, 2018 at 12:39
  • No extra explanation is required apart from that I have 3 pages and I want to send a parameter (no matters what and when) backwards
    – Dani
    Commented Feb 6, 2018 at 13:31

2 Answers 2

16

Last page:

 this.navCtrl.getPrevious().data.newGame = true;
 this.navCtrl.pop();

2nd page:

ionViewWillEnter() {
     this.newGame = this.navParams.get('newGame')|| null;
}
3
  • 1
    I didn't knew that you can do this, but it makes sense when you think about it. Why use events or callbacks if you can just grab and update whats inside the navStack directly Commented Jul 1, 2018 at 7:43
  • 1
    Excellent, best way, avoiding call back hell Commented Jul 10, 2018 at 11:11
  • 1
    Thanks for your answer, Dani! I wanted to use the default back arrow from Ionic navigation but passing data to the previous page on stack. What worked for me was using ionViewWillLeave() on the last page but without using pop() as it wasn't needed.
    – Eloy Ruiz
    Commented Jul 27, 2020 at 11:32
1

visit this if you can solve your problem this way, it suggests using pop() with params when going back from page 3 to page 2

pass in a callback when transitioning

// callback...
 myCallbackFunction = function(_params) {
     return new Promise((resolve, reject) => {
             resolve();
         });
 }

 // push page...
 this.navController.push(OtherPageComponent, {
    callback: myCallbackFunction
});

in the OtherPageComponent

 this.callback = this.navParams.get("callback")

 this.callback(param).then(()=>{
    this.navController.pop();
 });
5
  • between this option and the one using events which one would your say is the best one?
    – Dani
    Commented Feb 6, 2018 at 13:48
  • I tried this way but didn't work. I'm pretty sure is my fault but it may also be the version of Ionic. In other hand, the events work, but it seems that it needs more time to take the value from the event
    – Dani
    Commented Feb 6, 2018 at 14:08
  • And also why is this callback on previous page push(). Shouldn't it be inside ionViewDidEnter() for example?
    – Dani
    Commented Feb 6, 2018 at 14:21
  • 1
    Found a quicker way. I posted it. Might be a new way
    – Dani
    Commented Feb 6, 2018 at 14:33
  • I get error, Callback is not a function on ionic 4, "ERROR TypeError: this.callback is not a function"
    – Pankaj
    Commented Sep 28, 2018 at 5:16

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