简体   繁体   English

角度4中的数组内部访问数组

[英]access array inside array in angular 4

I'm using angular to create a web project that uses google's custom search engine to populate the web pages with the first ten results returned. 我正在使用angular创建一个Web项目,该项目使用谷歌的自定义搜索引擎以返回的前十个结果填充网页。 The data I get back from the API is in JSON format which I can assess and display using an interface. 我从API获得的数据为JSON格式,可以使用界面进行评估和显示。 I'm able access the array "items", my problem is I don't know how to access the array inside the items array. 我可以访问数组“ items”,我的问题是我不知道如何访问items数组内的数组。 Any help is welcome. 欢迎任何帮助。 Ps. 附言 i'm new to angular. 我是新来的角度。

  interface ISite{
    kind: string;
    title: string;
    htmlTitle: string;
    link: string;
    displayLink: string;
    srcImage: string;
    snippet: string;
}

  //Second interface to deal the api
  interface ISearchResponse {
  kind: string;
  context: string;
  items: ISite[];
  cse_images: string;
  company: string;
  snippet: string;
}

//and my service
  constructor(private _http: HttpClient) { }

   getSites(): Observable<ISearchResponse> {
    return this._http.get<ISearchResponse>(this._siteURL)
      .do(data => console.log('All: ' + JSON.stringify(data)))
      .catch(this.handleError);

  }
  private handleError(err: HttpErrorResponse) {
    console.log('SearchEngineService: ' + err.message);
    return Observable.throw(err.message);
  }
}

    //My html
    <h2>Search Results</h2>
<div class="list-group" *ngFor='let site of sites.items'>
  <a href="{{site.link}}" class="list-group-item list-group-item-action flex-column align-items-start active" >
    <div class="d-flex w-100 justify-content-between">
      <h4 class="mb-1">Title:  {{site.title}}</h4>
    </div>
    <h5>URL: {{site.link}}</h5>
    <p class="mb-1">Description: {{site.snippet}}</p>
  </a>
</div>

sample of the data form google api, I want to access the image in cse_image 数据表单google api的示例,我想访问cse_image中的图像

 "items": [
        {
            "kind": "customsearch#result",
        "title": "Sports News,Scores,Schedules,Standings,Stats,Photos",
            "pagemap": {
                "cse_image": [
                    {
                        "src": image.jpg"
                    }
                ]
            }
        },

Try this: 尝试这个:

<h2>Search Results</h2>
    <div class="list-group" *ngFor='let site of sites.items'>
      <a href="{{site.link}}" class="list-group-item list-group-item-action flex-column align-items-start active" >
        <div class="d-flex w-100 justify-content-between">
          <h4 class="mb-1">Title:  {{site.title}}</h4>
        </div>
        <h5>URL: {{site.link}}</h5>
        <p class="mb-1">Description: {{site.snippet}}</p>
    <img src={{site.pagemap.cse_image[0].src}}>
      </a>
    </div>

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

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