我正在尝试打印角表中的数据,数据来自JSON格式的节点。但在打印t时,即使在控制台中也显示未定义的数据。我也附上了JSON响应图像。
我正在使用API.Data从MongoDB获取数据,并使用它将数据发送到Angular,但问题是它没有打印在表中。
<table class="table table-bordered table-striped ">
<thead>
<tr>
<th>Username</th>
<th>Date registered</th>
<th>Role</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let x of data ">
<td>{{x.serial}}</td>
<td>2012/01/01</td>
<td>Member</td>
<td>
<span class="badge ba+dge-success">Active</span>
</td>
</tr>
</tbody>
</table>
API中的JSON Repsone
{
code: 0,
message: "success",
data: {
labels: [
{
_id: "605b262c36c70c354015a945",
serial: "12345",
sourceApi: "ASDASD",
resultApi: "ASDASD",
labelApi: "ASDASD",
__v: 0
},
{
_id: "605b26bd36c70c354015a946",
serial: "3333",
sourceApi: "sdfdg",
resultApi: "sdsf",
labelApi: "gggg",
__v: 0
},
{
_id: "605b26ea2e5ea63080dd4796",
serial: "3333",
sourceApi: "sdfdg",
resultApi: "sdsf",[enter image description here][1]
labelApi: "gggg",
__v: 0
}
]
}
}
TS文件代码
export class TablesComponent implements OnInit{
public data?:any;
constructor(private api:ApiServiceService) {
this.api.getDataApi().subscribe(data => {
this.data = data;
})
}
dtOptions: any = {};
ngOnInit(){
this.dtOptions = {
"paging" : true,
"ordering": true,
"info" : true
};
}
发布于 2021-03-30 06:44:05
您的JSON由一些不需要填充数据的其他数据组成。
在您的JSON标签中包含一个对象数组,所以我们必须将其赋给"data“变量
export class TablesComponent implements OnInit{
public data?:any;
constructor(private api:ApiServiceService) {
this.api.getDataApi().subscribe(data => {
this.data = data.data.labels;
})
}
https://stackoverflow.com/questions/66865767
复制