Ionic 视图上的离子/Angular 显示单个对象- ngIF*

mmvthczy  于 2022-12-09  发布在  Ionic
关注(0)|答案(1)|浏览(127)

我想寻求帮助,因为我遇到了一些问题。基本上,我从外部API(Strapi)获取一些对象,并在GET方法中接收它,但当我试图在视图上显示它时,ngIF*没有显示。
第一次
下面是我的代码:

文字详细信息-页面.html

<ion-header>
  <ion-toolbar color="primary">
    <ion-buttons slot="start">
      <ion-back-button defaultHref="/"></ion-back-button>
    </ion-buttons>
    <ion-title>{{ information?.name }}</ion-title>
  </ion-toolbar>
</ion-header>
 
<ion-content padding>
 
  <ion-card *ngIf="information">
    <ion-card-header>
      <ion-card-title>
        hello
        {{ information.id }}
      </ion-card-title>
      <ion-card-subtitle>
        {{ information.name }}
      </ion-card-subtitle>
    </ion-card-header>
  </ion-card>
  
</ion-content>

单词-详细信息-页面.ts

import { WordsService } from './../../services/words.service';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
 
@Component({
  selector: 'app-word-details',
  templateUrl: './word-details.page.html',
  styleUrls: ['./word-details.page.scss'],
})
export class WordDetailsPage implements OnInit {
 
  information = null;
  result: Object;
 
  /**
   * Constructor of our details page
   * @param activatedRoute Information about the route we are on
   * @param wordService The word Service to get data
   */
  constructor(private activatedRoute: ActivatedRoute, private wordService: WordsService) { }
 
  ngOnInit() {
    // Get the ID that was passed with the URL
    let id = this.activatedRoute.snapshot.paramMap.get('id');
 
    // Get the information from the API
    this.wordService.getDetails(id).subscribe(result => {
      this.information = result;
      console.log(this.information);
      alert(JSON.stringify(this.information));

    });
  }
 
  openWebsite() {
    window.open(this.information.Website, '_blank');
  }
}

文字.服务.ts

/**
  * Get the detailed information for an ID using the "i" parameter
  * 
  * @param {string} id wordID to retrieve information
  * @returns Observable with detailed information
  */
  getDetails(id) {
    return this.http.get(`${this.url}?id=${id}`);
  }
}
kninwzqo

kninwzqo1#

原因是你得到的是一个数组,而数组没有idname
您应该从

this.information = result;

this.information = result[0];

相关问题