简体   繁体   English

如何使用 Angular 对来自 API 的数据进行配对

[英]How to pair data from API using Angular

I'm learning Angular and trying to GET data from an API.我正在学习 Angular 并尝试从 API 获取数据。 The data object looks like数据对象看起来像

        {
      "count": 11, 
      "docs": [
        {
          "@context": {
            "@vocab": "http://purl.org/dc/terms/", 
            "LCSH": "http://id.loc.gov/authorities/subjects", 
            "aggregatedDigitalResource": "dpla:aggregatedDigitalResource", 
            "begin": {
              "@id": "dpla:dateRangeStart", 
              "@type": "xsd:date"
            }, 
            "collection": "dpla:aggregation", 
            "coordinates": "dpla:coordinates", 
            "dataProvider": "edm:dataProvider", 
            "dpla": "http://dp.la/terms/", 
            "edm": "http://www.europeana.eu/schemas/edm/", 
            "end": {
              "@id": "dpla:dateRangeEnd", 
              "@type": "xsd:date"
            }, 
            "hasView": "edm:hasView", 
            "isShownAt": "edm:isShownAt", 
            "name": "xsd:string", 
            "object": "edm:object", 
            "originalRecord": "dpla:originalRecord", 
            "provider": "edm:provider", 
            "sourceResource": "edm:sourceResource", 
            "state": "dpla:state", 
            "stateLocatedIn": "dpla:stateLocatedIn"
          }, 
          "@id": "http://dp.la/api/items/2709fce6f5fd80225fef5083c86d4bac", 
          "_id": "virginia--uva-lib:1042454", 
          "_rev": "1-d7cf9de9b01e3856fcac1a6590f426db", 
          "collection": {
            "@id": "http://dp.la/api/collections/virginia--744806"
          }, 
          "dataProvider": "Special Collections, University of Virginia Library, Charlottesville, Va.", 
          "hasView": {
            "@id:": "http://fedoraproxy.lib.virginia.edu/fedora/objects/uva-lib:1042454/methods/djatoka:StaticSDef/getStaticImage", 
            "rights": [
              "For more information about the use of this material, please go to http://search.lib.virginia.edu/terms."
            ]
          }, 
          "id": "2709fce6f5fd80225fef5083c86d4bac", 
          "ingestDate": "2013-03-27T12:55:23.353010", 
          "ingestType": "item", 
          "isShownAt": "http://search.lib.virginia.edu/catalog/uva-lib:1042454", 
          "object": "http://fedoraproxy.lib.virginia.edu/fedora/objects/uva-lib:1042454/methods/djatoka:StaticSDef/getThumbnail", 
          "originalRecord": {
            ...

Here's more info: https://pro.dp.la/developers/responses这里有更多信息: https ://pro.dp.la/developers/responses

In my Angular code, my app-components are:在我的 Angular 代码中,我的应用组件是:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from "@angular/common/http";

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.scss']
})
export class ScholarAPIComponent {
   title = 'image-gallery';
   public data:any = []
   constructor(private http: HttpClient) {
     
   }
    getData(){
         const url ='https://api.dp.la/v2/items?q=probability&api_key=<<<<<obscured ffor privacy>>>>>'
         this.http.get(url).subscribe((res)=>{
           this.data.docs = res;
         })
       }

and my html is我的html是

     <div class="libro" *ngFor="let d of data">
  <div class="desc">{{d.id}}</div>
  <div >{{d.object}}</div>
 </div>

I'm not getting any output on the html page;我在 html 页面上没有得到任何输出; however, when I console log the API data, I am getting it in the web browser console.但是,当我在控制台记录 API 数据时,我会在 Web 浏览器控制台中获取它。

I think I'm not understanding how to work with this data structure and for example call individual elements of the object data structure from the API call to loop through them on my HTML doc.我想我不了解如何使用此数据结构,例如从 API 调用中调用对象数据结构的各个元素以在我的 HTML 文档中循环遍历它们。

In your getData() method, you are not correctly assigning the response to the component property.在您的getData()方法中,您没有正确地将响应分配给组件属性。 In your component, you have declared an array named data and the response of the API is an object with a docs array field.在您的组件中,您已经声明了一个名为data的数组,并且 API 的响应是一个带有docs数组字段的对象。

This这个

this.http.get(url).subscribe((res)=>{
  this.data.docs = res;
})

should be应该

this.http.get(url).subscribe((res)=>{
  this.data = res.docs;
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM