在离子2中的页面之间传递数据



我是离子的新手,偶然发现了我在以下代码中遇到的问题。我有一个名为餐厅的页面。但是,这似乎并没有传递每个不同餐厅的细节。谁能告诉我我要去哪里?

这是页面。

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>

和餐厅.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));
  }
}

尝试使用(单个)引号周围的引号。

在餐厅尝试一下

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

和餐厅详细说法:

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.    
    }
}

我有一个直觉,您想要餐厅而不是ID:

<(click)="viewRestaurant(restaurant)">

这是因为您仅通过餐厅ID作为参数而不是整个餐厅详细信息。

<div class="card round" margin-bottom *ngFor="let restaurant of restaurants" (click)="viewRestaurant(restaurant)">

修改HTML,以便您传递整个对象数据。也将整个数据作为参数发送到推送

上的其他页面
 viewRestaurant(restaurant) {
    this.nav.push(RestaurantDetailPage, {id: restaurant})
  }

希望您只寻找

import {NavController} from 'ionic-angular';
constructor(private navController: NavController) {
this.navController.push(SecondPage, {
    param1: 'param1', param2: 'param2'
});
}

第二页:

constructor(private navController: NavController, private navParams: NavParams, private shareService: ShareService) {
this.parameter1 = navParams.get('param1'); 
this.parameter2 = navParams.get('param2');
}

相关内容

  • 没有找到相关文章

最新更新