![](/img/trans.png)
[英]Passing API data from one page to another (Ionic 2 + Angular 2)
[英]How to send the data from one page to another page? using ionic and angular
該項目有一個選項卡部分,其中我有兩個頁面 1. 食譜頁面 2. 收藏夾
在食譜頁面中,它們是食譜列表,我在每個列表的末尾添加了一個添加圖標按鈕。 如何通過單擊添加圖標按鈕將列表從食譜頁面發送到收藏頁面。
請幫助我
離子型,angular
如果您想將選項從一個頁面發送到另一個頁面,這是一個好方法。
首先,您必須列出選項,然后在選擇一個時,您推送到另一個頁面
在 index.html 中:
<ion-card *ngFor= "let elemento of resultado.list; let i = index"></ion-card>
食譜.js:
this.navCtrl.push( FavoritePage,{op: i} );
這種情況下最常用的解決方案是調用服務。 你調用一個服務來存儲你想要的數據,然后在另一個頁面上調用它。 如果您對如何做有任何疑問並且從未做過服務,請隨時提問。
我知道有兩種方法。
數據服務.ts
import { Injectable } from '@angular/core';
@Injectable()
export class dataService {
data: string;
setData(data) {
this.data = data;
}
getData(){
return this.data;
}
}
應用組件.ts
import { Component } from '@angular/core';
import { dataService } from './server.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(private dataService: dataService){}
getData() {
retrun this.dataService.getData();
}
}
2) 使用導航控制器
this.navCtrl.push(HomePage, {
data: userData
});
在主頁上,您可以像這樣訪問傳遞的數據
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.userData = navParams.get('data');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.