json - 使用 Ionic/Angular 在 HTML 中打印 Json 数据
问题描述
我正在使用 IONIC Angular 构建一个应用程序,并且我正在尝试以 HTML 格式打印结果。
从 console.log 它可以正常工作,但从视图中我无法打印数据
json接口
{
"matches1": {
"homeTeam": "Barcellona",
"awayTeam": "Real Madrid",
},
"matches2": {
"homeTeam": "PSG",
"awayTeam": "Lione",
}
}
主页.ts
export class HomePage {
matches1: any;
homeTeam1: any;
awayTeam1: any;
result1: any;
private apiurl = 'https://myapi.xxx';
constructor(private httpService: HttpClient) {
this.getdata();
}
getdata() {
this.httpService.get(this.apiurl).subscribe(res => {
this.item = res['matches1']['homeTeam'];
console.log(this.item); <-- this work in console log
}, (err: HttpErrorResponse) => {
console.log (err.message);
}
);
}
}
主页 html
<ion-item *ngFor="let item of items">
{{item.homeTeam}}
</ion-item>
谢谢!
解决方案
这应该做的工作:
export class HomePage {
matches1: any;
homeTeam1: any;
awayTeam1: any;
result1: any;
items: any;
private apiurl = 'https://myapi.xxx';
constructor(private httpService: HttpClient) {
this.getdata();
}
getdata() {
this.httpService.get(this.apiurl).subscribe(res => {
this.items = Object.keys(res).map(function(key, index) {
return res[key];
});
}, (err: HttpErrorResponse) => {
console.log (err.message);
}
);
}
}
推荐阅读
- latex - 乳胶对齐语法
- firebase - 即使我取消链接,电话仍然有价值(firebase)
- pyspark - Pyspark:过滤数据帧并将函数应用于偏移时间
- javascript - 将 Bootstrap JavaScript 添加到 Docusaurus 但有问题
- mysql - MySQL-如何在子查询中获取动态表名
- python - 如何使用 Pyspark 解决 spark-submit 上的 spark processnext() 错误
- r - 是否有类似于 Stata 中的 foreach 循环的 R 函数,用于根据现有变量的名称(或根)创建新变量?
- inno-setup - 根据 Inno Setup 中的在线列表检查 IP 地址
- python - 如何根据特定条件在熊猫中删除数据框中的重复行
- kubernetes - GCP 无法从外部访问 Kubernetes 外部负载均衡器 IP