Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic2 navController pop with params (CallBack)

I want to make a callback between two page. In the page 1, i have this code:

  DataInfo= [
    {
       Price: 0,
       ClosePrice: 0,
       UpdateTime:"",
       DefaultPrice:0
    }

  ] 

GetClosePrice(i):number{
return DataInfo[i].ClosePrice;
}

i want to get the value of 'i' from the page 2, How can i load the function GetClosePrice() when the navcontroller return to the page 1 (this.navCtrl.pop())

like image 503
Fray Avatar asked Feb 18 '17 18:02

Fray


2 Answers

SOURCE PAGE CLASS

this.navCtrl.push(Page,
{
    data: this.data,
    callback: this.getData
});

getData = data =>
{
  return new Promise((resolve, reject) => {
    for (let order of orders) {
      this.data = data;
    }
    resolve();
  });
};

TARGET PAGE CLASS

constructor(public navCtrl: NavController, public navParams: NavParams)
{
  this.callback = this.navParams.get('callback');
  this.data = this.navParams.get('data') || [];
}

sendData(event: any): void
{
  this.callback(this.data).then( () => { this.navCtrl.pop() });
}

TARGET PAGE TEMPLATE

<button ion-button (click)="sendData($event)">
like image 95
anonym Avatar answered Nov 02 '22 18:11

anonym


I answered similar question in Ionic forum. I just used Events listeners to achieve this behaviour.

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);
    });
}
like image 21
Sujit Kumar Singh Avatar answered Nov 02 '22 19:11

Sujit Kumar Singh