【问题标题】:How to get response (array of json) and put it in my html component in Angular 4如何获得响应(json数组)并将其放入Angular 4中的我的html组件中
【发布时间】:2018-01-16 10:13:22
【问题描述】:

我正在尝试在 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"
  }

],

}

我有这个输出的临时值,我需要接收这个响应并将它分发到它自己的标签上,我不知道如何

【问题讨论】:

  • 你试过什么?你能添加你的http调用代码吗?
  • @mast3rd3mon 我创建了服务,我可以创建函数来获得响应,但我无法接收这个数据数组并将其放入我需要的 html 标签中的变量

标签: json angular http response


【解决方案1】:

第一个解决方案,Angular 方式:

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>

【讨论】:

  • "// 在组件的变量中获取数据“我需要有关此变量的更多详细信息 ..?类型等等? &lt;p&gt;ID : {{liked.id}}&lt;/p&gt; 你是从哪里“点赞”的?
  • 不,您不需要有关这些变量的更多详细信息。您在 JS 中,您不必输入变量。我从上面的行中得到了这个liked ...
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 2020-10-02
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多