在Angular中使用httpClient获取嵌套对象



我正试图通过get请求将嵌套对象显示在模板中。问题是它显示了我的对象(嵌套对象(的主要属性。

export class User {
userId: number;
userName: string;
password: string;
firstName: string;
lastName: string;
role: number;
inforamation: Information;
}

这是我的模型,它可以显示用户属性,但不能显示信息。

getAllUsers(){
const header = new HttpHeaders({
'Content-Type':  'application/json'
});
return this.usersapiservice.getData(this.REST_API_SERVER, {headers : header});
}

ngOnInit(){
this.getAllUsers().subscribe((data: User[]) => {
this.users = data;
});
}

在我的html 中

{{users.firstname}} // work
{{users.information.adress}} // Does not work`

我看到了几个问题。在User类中,您将其中一个属性拼写错误为inforamation,但在模板中,该属性拼写正确为information

如果您有一个想要渲染的User数组,您可以使用Angular的*ngFor来迭代它们,如下所示:

<div *ngFor="let user of users">
{{user.firstName}}
{{user.information.address}}
</div>

最新更新