Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data between pages in Ionic 2

Tags:

I'm very new to Ionic and stumbled upon an issue I am having with my below code. I have a page called restaurant.html which lists the restaurants and when each of these items are clicked data is passed through (data pulled from a services file) to another page which should give the full details. However this doesn't seem to pass the details for each different restaurant. Can anyone tell me where I am going wrong?

Here are the pages.

restaurant.html

<ion-header>

  <ion-navbar color="restaurant-color">
    <button ion-button menuToggle>
      <ion-icon name="menu"></ion-icon>
    </button>
    <ion-title>Restaurants</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding class="restaurants attractions common-bg">
  <div class="card round" margin-bottom *ngFor="let restaurant of restaurants" (click)="viewRestaurant(restaurant.id)">
    <div class="card-header" [ngStyle]="{'background-image': 'url(' + restaurant.thumb + ')'}"></div>
    <div class="padding-xs">
      <h5>{{ restaurant.name }}</h5>
      <div class="rating">
        <ion-icon name="md-star" color="restaurant-color" *ngFor="let star of range(restaurant.rating)"></ion-icon>
        <ion-icon name="md-star" color="gray" *ngFor="let star of range(5 - restaurant.rating)"></ion-icon>
        <span color="gray">{{ restaurant.reviews.length }} reviews</span>
      </div>
      <span color="gray">Recommended for:</span>
      <div>
        <div class="pull-left">
          <span color="restaurant-color">{{ restaurant.scores[0].name }},</span>
          <span color="restaurant-color">{{ restaurant.scores[1].name }}</span>
        </div>
        <div class="pull-right">
          {{ restaurant.location.distance }} km
        </div>
        <div class="clear"></div>
      </div>
    </div>
  </div>
</ion-content>

and for restaurants.ts

import {Component} from "@angular/core";
import {NavController} from "ionic-angular";
import {RestaurantService} from "../../services/restaurant-service";
import {RestaurantDetailPage} from "../restaurant-detail/restaurant-detail";


@Component({
  selector: 'page-restaurants',
  templateUrl: 'restaurants.html'
})
export class RestaurantsPage {
  // list of restaurants
  public restaurants;

  constructor(public nav: NavController, public restaurantService: RestaurantService) {
    this.restaurants = restaurantService.getAll();
  }

  // view restaurant detail
  viewRestaurant(id) {
    this.nav.push(RestaurantDetailPage, {id: id})
  }

  // make array with range is n
  range(n) {
    return new Array(Math.round(n));
  }
}
like image 877
bluewavestudio Avatar asked Feb 28 '17 11:02

bluewavestudio


People also ask

How do I share data between pages in ionic?

To Pass data from Home page to About page we will need to import NavParams class. Since, I am using Ionic CLI to generate pages, class NavParams will already be imported in the about page.

How do you navigate from one page to another in ionic?

Navigation in Ionic works like a simple stack, where we push new pages onto the top of the stack, which takes us forwards in the app and shows a back button. To go backwards, we pop the top page off.

How do I pass data from one page to another page in ionic 4?

In Ionic 4 you can pass data with the modalController using ComponentProps and @Input() making it more suitable of a quick push/pop implementation.

How do you pass data to modal ionic?

Passing data to an Ionic modal This is as simple as calling the componentProps on our modalController create function. number: number = 3; const modal = await this. modalController.


1 Answers

Try with (single) quotes around your param key.

Try this on restaurants.ts

viewRestaurant(id) 
{
    this.nav.push(RestaurantDetailPage, {'id': id})
}

And in the restaurant-detail typescript:

import { NavController, NavParams} from 'ionic-angular';
export class .... {
    id: any;
    constructor(public navParams: NavParams){
        this.id = this.navParams.get('id');
        console.log(this.id);//The id that you passed must show up now.    
    }
}

I have a hunch that you want restaurant instead of an id here:

<(click)="viewRestaurant(restaurant)">
like image 134
alchi baucha Avatar answered Sep 23 '22 10:09

alchi baucha