Hi, may i know how did you do it?
i assigned _params to my local variable, however it shows undefined when i use ionViewDidEnter to console the local variable.
Can you help me ?
I achieved similar functionality using Events listeners using following codes -
MainPage-
import { NavController, Events } from 'ionic-angular';
import { OtherPage } from '../other/other';
export class MainPage{
constructor(private navCtrl: NavController,
private events: Events) { }
private pushOtherPage(){
this.events.subscribe('custom-user-events', (paramsVar) => {
/// Do stuff with "paramsVar"
this.events.unsubscribe('custom-user-events'); // unsubscribe this event
})
this.navCtrl.push(OtherPage); // Push your "OtherPage"
}
}
OtherPage-
export class OtherPage {
/// Under some function
this.navCtrl.pop().then(() => {
/// Trigger custom event and pass data to be send back
this.events.publish('custom-user-events', myCustomParams);
});
}
NavControllerās pop method already returns a promise which is resolved when the transition has completed. The following approach is based on that fact and it is different from the accepted answer, but works in a similar way:
Main page
export class MainPage {
dataFromOtherPage = null;
callback = data => {
this.dataFromOtherPage = data;
console.log('data received from other page', this.dataFromOtherPage);
};
constructor(public navCtrl: NavController) {}
openOtherPage() {
this.navCtrl.push('OtherPage', {
callback: this.callback
});
}
}
The other page:
export class OtherPage {
constructor(public navCtrl: NavController, public navParams: NavParams) {}
goToPreviousPage() {
this.navCtrl.pop().then(() => {
this.navParams.get('callback')('This is some data');
});
}
}
Thank you!
But for others, I must say that you need to
bind āthisā to the callback function.
Otherwise 'āthisā will be the other page model,
no the expected one.
I really dislike the suggestion in the previous post. It encourages overly tight coupling between pages, which makes apps hard to read, hard to test, hard to maintain. If you need to share data amongst pages, use a service provider. That way the relationship between each page and the service is clearly delineated. Poking around in framework navigation internals is a bad idea.