【发布时间】:2020-01-11 14:15:29
【问题描述】:
我的 json 如下所示:
{
"content": [
{
"id": 1,
"name": "test name",
"email": "test@test.com",
"img_url": "url",
"field1": [
{
"id": 10,
"userId": 1,
"registeredAt": "2020-01-09T22:21:23.272",
"amount": 200
}
],
"field2": [
{
"id": 11,
"userId": 1,
"registeredAt": "2020-01-09T22:21:46.113",
"amount": 200
}
],
"creationDateTime": "2020-01-05T00:00:00Z"
}
],
"page": 0,
"size": 30,
"totalElements": 1,
"totalPages": 1,
"last": true
}
我想用它填充我的angular 8 网站。
在我的api.service.ts 我有:
getMyStructure(): Observable<HttpResponse<MyStructure[]>> {
return this.http.get<MyStructure[]>(
localUrl, { observe: 'response' });
}
在app.components.ts 我有:
myStructure: MyStructure[] = [];
getMyStructure() {
this.api.getMyStructure()
.subscribe(function(resp) {
console.log(resp.body.length); //this prints undefined
console.log(resp.body) // this prints {content: Array, page: 0, size: 30, totalElements: 1, totalPages: 1, …}
this.myStructure = resp.body;
});
console.log("after");
console.log(this.myStructure.length); //this prints 0
现在,myStructure 是一个接口,其中包含:
export interface Shelter {
id: number;
name: string;
email: string;
img_url: string;
field1: string[];
field2: string[];
creationDateTime: string;
}
但myStructure 中的数据不知何故未填充。我不知道为什么,你能帮我吗?
【问题讨论】:
标签: javascript json angular angular8