Javascript 通过插值引用对象中的对象(角度2+;)
我的数据结构如下:Javascript 通过插值引用对象中的对象(角度2+;),javascript,html,css,angular,Javascript,Html,Css,Angular,我的数据结构如下: post : { content: 'content', creator: { name: 'Jones', id: 1, avatar: 'jones.png' } } 在HTML中,我希望通过插值显示,例如创建者的名称 我试过: {{ post.creator.name }} 但在应用程序中,此处是一个空字段。您必须声明对象,如下所示: post = { content: 'content', creator: { name: 'Jo
post : {
content: 'content',
creator: {
name: 'Jones',
id: 1,
avatar: 'jones.png'
}
}
在HTML中,我希望通过插值显示,例如创建者的名称
我试过:
{{ post.creator.name }}
但在应用程序中,此处是一个空字段。您必须声明
对象,如下所示:
post = {
content: 'content',
creator: {
name: 'Jones',
id: 1,
avatar: 'jones.png'
}
}
编辑
您可以向对象声明类型
post:post={…}
然后声明类:
export class Post {
content:string;
creator:Creator
}
export class Creator {
name: string;
id: number;
avatar: string;
}
请粘贴您的控制器或组件的类文件,这到底是什么问题name
有值,但它仍然没有显示在HTML中。@ashispal true,我想让它显示在HTML中。您不需要指定post:any。问题是OP没有将post设置为对象(post={content…})。@ל?post=new post({…})?无需new
仅:post
:post:post={}
: