简体   繁体   English

如何将数据从一页发送到另一页? 使用离子和 angular

[英]How to send the data from one page to another page? using ionic and angular

The project has a tab section in that i have two pages 1. recipe page 2. favorite该项目有一个选项卡部分,其中我有两个页面 1. 食谱页面 2. 收藏夹

in recipe page their is a list of recipes and i added a add icon button at the end of each list.在食谱页面中,它们是食谱列表,我在每个列表的末尾添加了一个添加图标按钮。 How to send list from the recipe page to favorite page by clicking the add icon button.如何通过单击添加图标按钮将列表从食谱页面发送到收藏页面。

pls help me请帮助我

Ionic, angular离子型,angular

If you want to send an option from one page to another, this is a good way.如果您想将选项从一个页面发送到另一个页面,这是一个好方法。

First, you have to list the options and when choosing one, you push to another page首先,您必须列出选项,然后在选择一个时,您推送到另一个页面

In index.html:在 index.html 中:

<ion-card *ngFor= "let elemento of resultado.list;  let i = index"></ion-card>

recipes.js:食谱.js:

this.navCtrl.push( FavoritePage,{op: i} );

The most use solution on that type of case is to call services.这种情况下最常用的解决方案是调用服务。 You call a service to store the data that you want, and then call it on the other page.你调用一个服务来存储你想要的数据,然后在另一个页面上调用它。 If you have any doubts on how to do it and never done a service, feel free to ask.如果您对如何做有任何疑问并且从未做过服务,请随时提问。

There are two ways that i know.我知道有两种方法。

  1. Using Services.使用服务。 (I prefer this one) (我更喜欢这个)

data.service.ts数据服务.ts

import { Injectable } from '@angular/core';

@Injectable()
export class dataService {
    data: string;

    setData(data) {
        this.data = data;
    }

    getData(){
       return this.data;
    }
}

App.component.ts应用组件.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) Using the NavController 2) 使用导航控制器

this.navCtrl.push(HomePage, {
      data: userData
});

On homepage you can access the passed data like this在主页上,您可以像这样访问传递的数据

constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.userData = navParams.get('data');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将API数据从一页传递到另一页(Ionic 2 + Angular 2) - Passing API data from one page to another (Ionic 2 + Angular 2) 如何使用Ionic 3和NavController使用Angular 4将数据从一页传递到另一页。 我需要传递第一页到第二页的输入并调用API - How to pass data from one page to another page using Ionic 3 and Angular 4 using NavController. I need to pass input of 1st to 2nd page and callAPI 如何将数据从一个页面传递到另一个页面,以便在Ionic 2中进行导航 - How to pass data from one page to another for Navigation in Ionic 2 如何使用角度片段将数据从一个页面传递到另一个页面? - How to pass data from one page to another page using angular fragments? Ionic 4如何从另一个页面获取数据 - Ionic 4 How to get data from another page OnClick将项目从一页添加到另一页(Ionic 2 + Angular 2) - OnClick add items from one page to another (Ionic 2 + Angular 2) 在 angular 离子中将参数从一页传递到另一页 - pass params from one page to another in angular ionic 如何在离子2中将一页移动到另一页? - How to move one page to another page in ionic 2? 如何将数据传递到 ionic-Angular 中的另一个页面 - How to pass data to another page in ionic-Angular 来自Start Page的使用Ionic 2 + Angular 2的访问数据 - Acess data from Start Page using Ionic 2 + Angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM