angular - 以角度 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>
请让我知道我该怎么办?...
解决方案
如果 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>
推荐阅读
- tsql - 为每个参数设置加密
- angular - 如何避免检测到循环依赖中的警告:使用模态
- r - 如何在 R 中创建漏斗视图
- java - ConstraintLayout layoutParams setMargin() notWorking
- c# - 使用 WebClient 复制到 FTP 后文件正在使用错误
- flutter - 如何使用颤动在页面加载时淡入图像过渡?
- java - 使用循环替换 .txt 文件或 .xml 文件中的文本
- matlab - 加快 MATLAB 中 netcdf 数据的读入
- python - Django 重定向:没有错误,但没有重定向
- jenkins - 有没有办法通过api访问Jenkins自定义环境变量?