首页 > 解决方案 > 以角度 8 从简单 api 打印数据

问题描述

我在下面有一个 API URL,我必须在我的 html 组件上打印数据,请告诉我如何打印的方法。我有以下方法,所以我必须打印字符串,例如。page_tilte

resData;
newsdata;

ngOnInit() {
const url="https://tiavik.com/maken/api/aboutUs";
this.http.get(url).subscribe(res=>{
  this.resData=res;
  this.newsdata=this.resData.Data
  console.log(res);
}) 

我在我的 html 中使用它:-

<div class="h3 block-title" *ngIf="newsdata">
<div *ngFor="let news of newsdata">
  {{news?.page_title}}
</div>
</div>

请让我知道我该怎么办?...

标签: angularapi

解决方案


如果 API 的响应只是一个对象,那么我们可以直接从对象中读取数据,而不是迭代,就像下面的代码快照一样。

result;

ngOnInit() {
 const url="https://tiavik.com/maken/api/aboutUs";
 this.http.get(url).subscribe(res=>{
 this.result = res.data;
});

直接读取result模板中的数据。

<div class="h3 block-title">
 {{ result.page_title }}
</div>

推荐阅读