html - 如何使用 Angular 6 在 html 中显示动态 json 数据?
问题描述
我的网络服务返回一个 json 数据,如下所示。
[{ "Key" : 001,
"Record" : {"id":"001",
"name" : "qwerty"}
},
{ "Key" : 003,
"Record" : {"id":"003",
"name" : "asdfg"}
}]
现在我需要以表格格式显示它。通常在jquery中我用来动态创建一个表并将一个div id分配给一个表,然后用动态创建的表替换它。
我的组件.ts:
export class CatComponent extends Lifecycle {
constructor(
private $modal: $ModalManagerService,
private http: HttpClient
) {
super();
}
_initialize(): void {
this.http.get('http://127.0.0.1:3000/query/aa',{responseType:"json"}).subscribe(
response => {
console.log("data :"+response);
var sample=JSON.stringify(response);
});
}
}
$("#divv").html(content);
在角度 6 中我应该如何显示?
解决方案
TS
export class CatComponent extends OnInit{
public data: any;
constructor(
private $modal: $ModalManagerService,
private http: HttpClient
) {
super();
}
ngOnInit(): void {
this.http.get('http://127.0.0.1:3000/query/aa',{responseType:"json"}).subscribe(
response => {
this.data = response;
console.log("data :"+response);
var sample=JSON.stringify(response);
});
}
}
HTML
<table>
<thead>
<tr>
<td>Key</td>
<td>ID</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<tr *ngFor ="let d of data;">
<td>{{d.Key}}</td>
<td>{{d.Record.id}}</td>
<td>{{d.Record.name}}</td>
</tr>
</tbody>
</table>
推荐阅读
- javascript - Vue.js 子组件将数据发送到父对象中
- java - Java fxml从表单返回值
- c# - 如何使用对象来保存数组并针对另一个数组使用?
- php - 在文本中突出显示关键字 - 优化
- git - Git pull 不更新远程的最后一次提交
- ghostscript - 如何防止 GhostScript 裁剪 pdf 文件?
- arrays - ReactJS:带有条件表达式的嵌套 .map 以匹配值
- python - matplotlib中条形图中的断轴斜线标记?
- css - CSS not:last-child 或 last-child 未按预期选择
- php - Laravel 无法访问除根以外的其他路由(使用 XAMPP)