簡體   English   中英

如何獲取響應(json數組)並將其放在Angular 4中的我的html組件中

[英]How to get response (array of json) and put it in my html component in Angular 4

我正在嘗試在JSON響應中獲取數組數據。 以下是我的JSON響應,我需要將所有數據獲取到組件中的html標記。

   {
  data : {
  "topLikedMedia" : [

  {
    "id": "1546567845943506613_3718981156",
    "type": "image",
    "user": {
      "id": "3718981156",
      "username": "agoramonitor",
      "full_name": "AgoraMonitor",
      "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/18809269_476795959342067_7353566623065702400_n.jpg"
    },
    "tags": [
      "instagramers",
      "content",
      "socialmedia",
      "marketing",
      "branding",
      "instagram"
    ],
    "location": null,
    "comments": {
      "count": 2
    },
    "formatted_comments_count": "2",
    "created_time": "1498585275",
    "formatted_time": "Tue, Jun 27, 2017 7:41 PM",
    "diff_humans_time": "4 months ago",
    "link": "https://www.instagram.com/p/BV2g0MGgPa1/",
    "likes": {
      "count": 154
    },
    "formatted_likes_count": "154",
    "images": {
      "thumbnail": {
        "width": 150,
        "height": 150,
        "url": "https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/c244.0.591.591/19533988_862713503881059_8677706625265434624_n.jpg"
      },
      "low_resolution": {
        "width": 320,
        "height": 175,
        "url": "https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/19533988_862713503881059_8677706625265434624_n.jpg"
      },
      "standard_resolution": {
        "width": 640,
        "height": 350,
        "url": "https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/19533988_862713503881059_8677706625265434624_n.jpg"
      }
    },
    "caption": "Whether you want to drive leads to your homepage or encourage customer engagement ",
    "userHasLiked": false,
    "filter": "Normal"
  }

],

}

我有此輸出的臨時文件,我需要接收此響應並將其分發到自己的標簽上,我不知道如何

第一個解決方案,角度方式:

getTopLiked() { 
  this._dashboardService.getTopPosts()
    .subscribe(res => { 
      // Get your data in your component's variable
      this.data = res.json();
    });
}

在您的HTML中

<div *ngIf="data">
  <div *ngFor="let liked of data.topLikedMedia">
    <p>ID : {{liked.id}}</p>
    <!-- And you do every other field like that -->
  </div>
</div>

第二種解決方案,舊的JavaScript方法

getTopLiked() { 
  this._dashboardService.getTopPosts()
    .subscribe(res => { 
      this.createMarkup(res.json());
    });
}

createMarkup(data: Object) {
  this.markup = '';
  for (let liked of data.topLikedMedia) {
    this.markup += `<p>ID : ${liked.id}</p>`;
    // add the other fields like that
  }
}

在您的HTML中

<div *ngIf="markup">
  <div [innerHTML]="markup"></div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM