来自用户Angular的嵌套数据



我有来自控制台的后台数据:

user { courses:
{ createdAt: 2017-10-24T14:35:17.562Z,
_id: 59ef4fcf941e450ccc132f13,
title: 'My course',
__v: 0 },
_id: 5b38e3bc9dbf9113560a2d4c,
username: 'marcos',
email: 'marcos@marcos.com'

在用户页面中只显示:_id、用户名和电子邮件,但不显示数组进程。但我没有出现在用户页面上,有人能帮我吗?

Courses是一个对象,它不是一个数组,您是如何访问它的?

user.courses.createdAt
user.courses._id

是访问子对象属性的语法。

Courses是一个对象,您需要使用要绑定的属性进行访问

<hello name="{{ user.username }}"></hello>
<p>
Your email is {{user.email}} 
your course is {{user.courses.title}}
</p>

STACKBLITZ演示

您必须使用属性名称访问课程对象

<div *ngFor="let user of userList">
{{user._id}}
{{user.username}}
{{user.email}}
<p>
{{user.courses.createdAt}}
{{user.courses._id}}
{{user.courses.title}}
{{user.courses.__v}}
</p>
</div>

这就是访问用户列表对象的方式。如果它只是一个对象,那么它可以通过属性名称访问

<div>
{{user._id}}
{{user.username}}
{{user.email}}
<p>
{{user.courses.createdAt}}
{{user.courses._id}}
{{user.courses.title}}
{{user.courses.__v}}
</p>
</div>

这很有效。我在我的后端中转换了数组中的obj-json。在Frontend中,我使用了{{courses.title}}感谢reggards!

最新更新