2016-04-21 84 views
4

我通過http獲取數據。我想將數據傳遞給PlacesListPage。在我的數據中有「ID,名稱,類別......」。我想在PlacesListPage使用theese這樣的:{{} xxx.id},{{xxx.name}} ...請幫我... XXX - 例如)如何通過ionic2傳遞數據

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; 
     }); 

} 
+0

什麼是'PlacesListPage'?這是一項服務嗎?它是一個自定義元素嗎?你在'places.html'裏面使用它嗎? – kabaehr

+0

是的。這是自定義頁面。我進口頂部。第4行 –

+0

你能提供它的代碼嗎? – kabaehr

回答

2

使用NavController將數據傳遞到頁面,你找到它:

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

然後使用NavParams在PlacesListPage訪問數據:

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

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

+0

但我的數據是一個對象。我無法將對象數據傳遞到其他頁面 –

+0

您應該可以,您是否收到錯誤? –

+0

是的。當我點擊進入另一個頁面的數據。應用程序不起作用,並顯示我在控制檯日誌上的錯誤 –

7

您可以使用此方法還有:

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

,比

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

謝謝。我解決了我的問題。 –

+0

如果它幫助你,你可以接受:) –