嗨,我想只获取我单击的项目的JSON数据库,当我单击该项目时,我没有得到任何JSON数据,相反它在控制台中显示为“ undefined”。希望你能帮助我。这是我的代码:

Featured.ts

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

import { TestProviderPage } from '../testprovider/testprovider';
import { ProductPage } from '../product/product';

@Component({
  selector: 'page-featured',
  templateUrl: 'featured.html',
  providers: [TestProviderPage]
})
export class FeaturedPage {
  products: any;
  constructor(public navCtrl: NavController, public testProvider: TestProviderPage) {
    this.loadItems();
  }

  goToProductPage(item){
    this.navCtrl.push(ProductPage, { item: item });
  }

  loadItems(){
    this.testProvider.loadpatrick().then(data => {
    this.products = data;
    console.log(this.products);
    });
  }

}



product.ts

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

import { MorePage } from '../more/more';

@Component({
  selector: 'page-product',
  templateUrl: 'product.html'
})
export class ProductPage {
  item: any;
  tabBarElement: any;
  constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.tabBarElement = document.querySelector('.tabbar.show-tabbar');
    this.item = this.navParams.get('product');
    console.log(this.item);
  }

}

最佳答案

在FeaturePage中,将参数项传递给ProductPage。因此,您只需要检索相同的参数。

FeaturePage.ts

 this.navCtrl.push(ProductPage, { item: item })


产品页面

this.navParams.get('item')

关于javascript - 根据我点击了ionic 2的项目获取JSON数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44986687/

10-13 00:42