在ionic2中将项目点击从一个页面传递到另一个页面

时间:2016-08-26 09:31:50

标签: angular ionic-framework

我正在使用ionic2,我有一个静态的项目列表(离子项)。

当我点击某个项目时,我需要将变量(在本例中为wcyear)传递给第二页。 问题是如果我打印到控制台我传递的变量(wcyear)我得到了未定义。

注意:如果我用home.ts中的常量字符串替换wcyear(即year:"helloworld"),那么我会看到" helloworld"打印在我的控制台中。

这让我觉得我的home.html有错误,但我看不到它。

home.html的

<ion-header>
  <ion-navbar>
    <ion-title>
      Ionic 2 app
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
<ion-list>
  <ion-item let wcyear="2014" (click)="goToYear(wcyear)">
    <ion-avatar item-left>
      <img src="img/2014.png">
    </ion-avatar>
    <h2>2014</h2>
    <h3>some text</h3>
  </ion-item>
  <ion-item let wcyear="2010" (click)="goToYear(wcyear)">
    <ion-avatar item-left>
      <img src="img/2010.png">
    </ion-avatar>
    <h2>2010</h2>
    <h3>some text</h3>
  </ion-item>
  <ion-item let wcyear="2006" (click)="goToYear(wcyear)">
    <ion-avatar item-left>
      <img src="img/2006.png">
    </ion-avatar>
    <h2>2006</h2>
    <h3>some text</h3>
  </ion-item>
  <ion-item let wcyear="2002" (click)="goToYear(wcyear)">
    <ion-avatar item-left>
      <img src="img/2002.png">
    </ion-avatar>
    <h2>2002</h2>
    <h3>some text</h3>
  </ion-item>
</ion-list>

home.ts

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {YearDashboardPage} from '../year-dashboard/year-dashboard';

@Component({
  templateUrl: 'build/pages/home/home.html'
})

export class HomePage {
  constructor(public navCtrl: NavController) {
    this.navCtrl = navCtrl;
  }

  goToYear(wcyear){
    this.navCtrl.push(YearDashboardPage, {
      year: wcyear,
    });
  }

}

年-dashboard.ts

import { Component } from '@angular/core';
import { NavController, NavParams} from 'ionic-angular';


@Component({
  templateUrl: 'build/pages/year-dashboard/year-dashboard.html',
})
export class YearDashboardPage {

  selectedYear
  constructor(private navCtrl: NavController, private navParams: NavParams) {
     console.log(navParams.get('year'));  // I get undefined
  }

}

1 个答案:

答案 0 :(得分:0)

而不是

console.log(navParams.get('year')); 

使用

console.log(this.navParams.get('year'));..