如何在ionic2中传递数据

时间:2016-04-21 20:50:47

标签: angular ionic2

我通过http获取数据。我想将数据传递给PlacesListPage。有" id,name,category,..."在我的数据中。我想在PlacesListPage中使用theese,如下所示:{{xxx.id}},{{xxx.name}} ...请帮帮我... xxx - 例如)

import {Page, NavController, NavParams} from 'ionic-angular';
import {Http} from 'angular2/http';
import 'rxjs/Rx';
import {PlacesListPage} from '../places-list/places-list';
/*enter code here
  Generated class for the PlacesPage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Page({
  templateUrl: 'build/pages/places/places.html',
})
export class PlacesPage {
  static get parameters() {
    return [[NavController], [Http], [NavParams]];
  }


  constructor(nav, http, navParams) {
    this.nav = nav;
    this.http = http.get('https://api.myjson.com/bins/2ud24').map(res => res.json()).subscribe(
        (data) => {
          this.data = data;
        });

}

2 个答案:

答案 0 :(得分:7)

你也可以使用这种方法:

let something = { test: 1 };
this.nav.push(existingPaymentModal, {something});

而不是

constructor(private navParams: NavParams) {
        this.something = this.navParams.get("something");
 }

答案 1 :(得分:2)

使用NavController在导航到页面时将数据传递给页面:

this.nav.push(PlacesListPage, {
    xxx: this.data
});

然后在PlacesListPage中使用NavParams来访问数据:

constructor(navParams) {
    this.xxx = navParams.data.xxx;
}

http://ionicframework.com/docs/v2/api/components/nav/NavParams/