2

Am new to ionic, and am currently stuck in this problem, I'm trying to pass an object that have this structure

{
car: {...}
reservations:[{...},{...},...]
}

I did this code but a was able to send only the car object but couldnt send the reservation array.

page1 (sender):

  //class success object

  class SuccessObject {
    success: any;

    public constructor(success: any) {
        this.success = success;
    }
  }
  public getCar(id){
    let loader = this.loading.get('Chargement de la reservation...');
    loader.present();
    this.data.getCar(id).subscribe(
        data => {
          //DATA is full with car infos and its reservations
          let infos: SuccessObject = new SuccessObject(data);
            this.navCtrl.push(CarinfosPage, {
                'car': infos.success.car,
                'res': infos.success.reservation,
            });
            loader.dismiss();
        },
        err => {
            loader.dismiss();
        }
    );  
  }

page 2 (reciever):

export class CarinfosPage {

  public data;
  reservations = <any>{};
  car = <any>{};

  constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.reservations = this.navParams.data;
    this.car = this.navParams.get('car');
  }

  ionViewDidLoad() {
    console.log(this.car); // i get car infos here
    console.log(this.reservations); // i get undefined here
  }

}
S4L4H
  • 402
  • 1
  • 5
  • 21

2 Answers2

3

you didn't get this.navParams.get('res');

    this.reservations = this.navParams.get('res');


 ionViewDidLoad() {
    console.log(this.car); // i get car infos here
    console.log(this.reservations); // i get undefined here
  }
Chanaka Weerasinghe
  • 5,404
  • 2
  • 26
  • 39
1

Instead of passing the big payload of an array to the second page through the router, an alternative is send just the id; then have a data service that will be used by the second page to retrieve the data and get all the information that it needs.

Page 1 (sender)

  public getCar(id){
     this.navCtrl.push(CarinfosPage, { id: id });
  }

Page 2 (receiver):

export class CarinfosPage implements OnInit {
  public data;
  reservations = <any>{};
  car = <any>{};

  // TODO inject the data service
  constructor(
      public navCtrl: NavController,
      public navParams: NavParams,
      public dataService: DataService
  ) {}

 ngOnInit() {
    const id = this.navParams.get('id');

    this.dataService.getCar(id).subscribe(
        data => {
         // TODO Get the car and reservations information here
          ...
        },
        err => {
           ...
        }
    );  
 }
}
Vivens Ndatinya
  • 154
  • 1
  • 4