Ionic2 navController pop with params(CallBack)

Fra*_*ray 6 callback ionic2

我想在两个页面之间进行回调.在第1页中,我有这个代码:

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

  ] 

GetClosePrice(i):number{
return DataInfo[i].ClosePrice;
}
Run Code Online (Sandbox Code Playgroud)

我想从页面2获取'i'的值,当navcontroller返回到页面1时,如何加载函数GetClosePrice()(this.navCtrl.pop())

ano*_*nym 23

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();
  });
};
Run Code Online (Sandbox Code Playgroud)

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() });
}
Run Code Online (Sandbox Code Playgroud)

TARGET PAGE TEMPLATE

<button ion-button (click)="sendData($event)">
Run Code Online (Sandbox Code Playgroud)


Suj*_*ngh 8

我在Ionic论坛上回答了类似的问题.我只是习惯于Events listeners实现这种行为.

主页-

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

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);
    });
}
Run Code Online (Sandbox Code Playgroud)