简体   繁体   English

Ionic 4 - 如何将 id 参数传递到另一个页面并从外部 api 检索 id 数据

[英]Ionic 4 - How to pass id parameter to another page and retrieve id's data from external api

in advance sorry for the dummy question, but I can't resolve this problem.提前抱歉这个虚拟问题,但我无法解决这个问题。 So I have a list of categories that comes from a nodeJS api.所以我有一个来自 nodeJS api 的类别列表。 I want to retrieve the subcategories according to the id, from the database.我想根据 id 从数据库中检索子类别。 Application is communicating with api through a service.应用程序正在通过服务与 api 通信。 On the database side, tables "category" and "subcategory" are related through a foreign key which is in "subcategory" table.在数据库方面,表“类别”和“子类别”通过“子类别”表中的外键关联。 I know my sql request I made on the api is ok because I tested it on postman.我知道我在 api 上提出的 sql 请求没问题,因为我在 postman 上进行了测试。 The problem is for now, I retrieve all the subcategories, and not only those related to my id's.现在的问题是,我检索了所有子类别,而不仅仅是那些与我的 id 相关的子类别。

api.service.ts api.service.ts

  getDataCategories(): Observable<any> {
    return this.http.get(categoriesUrl, httpOptions).pipe(
      map(this.extractData),
      catchError(this.handleError));
  }

  getDataSubCategories(): Observable<any> {
    return this.http.get(subCategoriesUrl, httpOptions).pipe(
      map(this.extractData),
      catchError(this.handleError));
  }

categoriespage.ts分类页面.ts

export class CategoriesPage implements OnInit {

  categories: any=[];
  id:any;

  constructor(public api: ApiService, private route: ActivatedRoute) { }

  ngOnInit() {
    this.api.getDataCategories().subscribe(res=>{
      this.categories= Object.values(res);
      console.log(this.categories);
      return this.categories;
    })
  }

categoriesdetailpage.html分类详情页。html

<ion-col size="6">
      <ion-list *ngFor="let data of subCategories[0]">
        <ion-item>{{ data.name }}</ion-item>
      </ion-list>
    </ion-col>

categoriesdetailpage.ts分类详情页.ts

export class CategoryDetailPage implements OnInit {

  categoriesdetails:any=[];

  constructor(public api: ApiService, private route: ActivatedRoute) { }

  ngOnInit() {

    this.api.getDataSubCategories().subscribe(res=>{
      this.categoriesdetails= Object.values(res);
      console.log(this.categoriesdetails);
      return this.categoriesdetails;
    })

  }

Thanks for your help !!谢谢你的帮助 !!

Not sure I understood your question.不确定我是否理解你的问题。 You want to pass an ID to a different page and then use this ID to retrieve the subcategories from the database?您想将 ID 传递到不同的页面,然后使用此 ID 从数据库中检索子类别? If that is the case you cand do something like this:如果是这种情况,您可以执行以下操作:

api.service.ts: api.service.ts:

  getDataCategories(): Observable<any> {
    return this.http.get(categoriesUrl, httpOptions).pipe(
      map(this.extractData),
      catchError(this.handleError)
    );
  }

  getDataSubCategories(id: string): Observable<any> {
    return this.http.get(subCategoriesUrl + "?id_subcategory=" + id, httpOptions).pipe(
      map(this.extractData),
      catchError(this.handleError)
    );
  }

categoriespage.ts:类别页面.ts:

import { NavigationExtras } from '@angular/router';
import { NavController } from '@ionic/angular';

export class CategoriesPage implements OnInit {

  categories: any=[];
  id:any;

  constructor(public api: ApiService, private route: ActivatedRoute) { }

  ngOnInit() {
    this.api.getDataCategories().subscribe(res=>{
      this.categories= Object.values(res);
      console.log(this.categories);
      return this.categories;
    });
  }

  onSelectedCategory(id: any) {
    let extras: NavigationExtras = {
      state: {
        id_category: id
      }
    };
    this.navController.navigateForward('/categorie-detail-page', extras);
  }
}

categoriesdetailpage.ts:类别detailpage.ts:

import { Router } from '@angular/router';

export class CategoryDetailPage implements OnInit {

  categoriesdetails:any=[];

  constructor(private router: Router, public api: ApiService, private route: ActivatedRoute) {
    this.state = this.router.getCurrentNavigation().extras.state;
    if (this.state != undefined) {
      this.api.getSubcategories(this.state['id_category']).subscribe(res => {
        this.categoriesdetails= Object.values(res);
        console.log(this.categoriesdetails);   
      });
    }
  }
}

Of course you have to make a few changes to this code, but I think it can point you into the right direction.当然,您必须对此代码进行一些更改,但我认为它可以为您指明正确的方向。

暂无
暂无

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

相关问题 如何在同一页面中将 id(Parameter ) 从一个地方传递到另一个地方? - How to pass id(Parameter ) from one place to another in same page? 如何将我的 ionic 应用程序连接到同一页面中的两个不同的 API URL? 如何将第一个 url 数据中的 id 传递给第二个 url? - How can I connect my ionic app to two different API URLS in the same page? how to pass the id from the first url's data to the second url? 如何将数据从一个页面传递到另一个页面,以便在Ionic 2中进行导航 - How to pass data from one page to another for Navigation in Ionic 2 如何将带有 id 的数组从一个组件传递到另一个组件(角度) - How to pass an array with id's from one component to another (angular) 如何访问来自 API 响应的 ID,应该用作 deleteApp API 的参数传递 - How to access id which is coming from API response and should be used to pass as parameter for deleteApp API Ionic 2-如何将id参数传递到“ http get”函数中,以便仅显示特定用户的数据,而不是全部显示 - Ionic 2 - How do I pass id parameter into “http get” function so that it only display data of specific user and not all 如何使用 Angular 将 ID 一页传递到另一页? - How to Pass ID one page to another page using Angular? 通过 ID 将 API 数据传递给 MatDialog - pass API data to a MatDialog by ID Ionic 4如何从另一个页面获取数据 - Ionic 4 How to get data from another page 如何将数据传递到 ionic-Angular 中的另一个页面 - How to pass data to another page in ionic-Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM