无法使用navctrl和navparams将参数从一个页面传递到另一个页面

时间:2017-11-30 17:13:38

标签: javascript typescript

我有一个问题,我无法使用navctrl和navparams向我想要的页面传递以'any'开头的值..

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

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase} from 'angularfire2/database';
import {RekomendasiPage} from '../rekomendasi/rekomendasi'

/**
 * Generated class for the LaporankesehatanPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-laporankesehatan',
  templateUrl: 'laporankesehatan.html',
})
export class LaporankesehatanPage {

  id: string;
  email : string;
  fullname : string;
  jeniskelamin : string;
  usia : number;
  berat : number;
  tinggi : number;
  bmi :number;
  hasil : string;
  category : number;
  kebutuhan : string;
  karbohidrat : number;
  protein_nabati : number;
  protein_hewani : number;
  lemak : number;
  gizi : any;
  test : string;


  constructor(private db : AngularFireDatabase,public fire :AngularFireAuth,public navCtrl: NavController, public navParams: NavParams) {
    this.id =this.fire.auth.currentUser.uid;
    this.db.object('/user/'+this.id).subscribe(data => {              //ambil data user
      this.fullname = data.fullname;
      this.jeniskelamin = data.jeniskelamin;
      this.usia = data.usia;
      this.berat = data.berat;
      this.tinggi = data.tinggi;
      this.bmi = Number((this.berat/((this.tinggi/100)*(this.tinggi/100))));

// menghitung bmi
      if (this.bmi < 18.5 )
      {
        this.hasil="Berat badan kurang";
        this.category = 1;
      } 
      else if (this.bmi >= 18.5 && this.bmi <25)
      { 
        this.hasil = "Normal";
        this.category = 2;
      } 
      else if (this.bmi >= 25 && this.bmi <30 )
      {
        this.hasil = "Berat badan lebih";
        this.category = 3;
      }
      else
      {
        this.hasil = "obesitas";
        this.category =4;
      }
      console.log(this.hasil);
      console.log(this.bmi);    


    })
//mengecek kebutuhan porsi gizi
    if(this.category == 1)
    {
      this.kebutuhan = "3 Karbohidrat, 4 Protein hewani, 4 protein nabati, 2 lemak "
      this.gizi.karbohidrat=3;
      this.gizi.protein_hewani = 4;
      this.gizi.protein_nabati=4;
      this.gizi.lemak=2;
    }

    else if(this.category == 2)
    {
      this.kebutuhan = "3 Karbohidrat, 4 Protein hewani, 3 protein nabati,2 lemak "
      this.gizi.karbohidrat=3;
      this.gizi.protein_hewani = 4;
      this.gizi.protein_nabati=3
      this.gizi.lemak=2;
    }

    else if(this.category == 3)
    {
      this.kebutuhan = "3 Karbohidrat, 3 Protein hewani, 3 protein nabati, 2 lemak "
      this.gizi.karbohidrat=3;
      this.gizi.protein_hewani = 3;
      this.gizi.protein_nabati=3;
      this.gizi.lemak=2;
    }
    else
    {
      this.kebutuhan = "2 Karbohidrat, 3 Protein hewani, 3 protein nabati, 1 lemak "
      this.gizi.karbohidrat=2;
      this.gizi.protein_hewani = 3;
      this.gizi.protein_nabati=3;
      this.gizi.lemak=1;
    }
    console.log(this.gizi)

  }


  ionViewDidLoad() {
    console.log('ionViewDidLoad LaporankesehatanPage');
  }

  cekMakanan(gizi)
  {
    this.navCtrl.push(RekomendasiPage,this.gizi);
  }

}

我想将karbohidrat值,protein_hewani值,protein_nabati值和lemak值填充到一个名为gizi的变量中。 错误说:uncaught(承诺):无法设置属性'Karbohidrat'未定义

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

阅读你的代码,我可以看到这个任务:

this.gizi

this.gizi = {}; 初始化之前。即。

this.gizi = new Gizi();

Create Table #Order
(OrderId int identity primary key,
TypeId char(1),
Total decimal(5,2)
)

Create Table #OrderLine
(OrderLineId int identity,
OrderId int constraint FK_OrderId foreign key (OrderId) references [#Order](OrderId),
LineItem int,
TypeId char(1),
Item varchar(30),
Price decimal (5,2)
)

Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Tennis Racket', 120)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Red Dress', 80)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Basketball', 30)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Dress Shirt', 60)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Pingpong Balls', 10)
Insert into #OrderLine (TypeId,Item, Price)
Values('S', 'Soccer Ball', 25)
Insert into #OrderLine (TypeId,Item, Price)
Values('C', 'Shorts', 20)

Insert into #Order(TypeId, Total)
Select  #OrderLine.TypeId
        ,SUM(#OrderLine.Price)
From    #OrderLine
Group by #OrderLine.TypeId

Select * From #Order
Select * From #OrderLine

Drop Table #Order
Drop Table #OrderLine