Ionic2 navController pop with params(CallBack)

时间:2017-02-18 18:11:27

标签: callback ionic2

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

snd_pcm_frames_to_bytes

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

3 个答案:

答案 0 :(得分:22)

<强> 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)">

答案 1 :(得分:7)

我在Ionic forum回答了类似的问题。我只是用Events listeners来实现这种行为。

<强>的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);
    });
}

答案 2 :(得分:0)

试试这个 - ionic2 pop with params