0

I have a data structure like this:

post : {
 content: 'content',
 creator: {
  name: 'Jones',
  id: 1,
  avatar: 'jones.png'
 }
}

In HTML i want to display by interpolation for example the name of the creator.

I tried that:

{{ post.creator.name }}

But in app, at this place is an empty field.

3
  • 2
    could you please paste your controller or class file of the component Commented Nov 28, 2018 at 11:07
  • what exactly is the problem here? name has value but still, it is not appearing in HTML. Commented Nov 28, 2018 at 11:15
  • @ashishpal true, I want to make it appears in html Commented Nov 28, 2018 at 11:32

1 Answer 1

1

You have to declare object as below:

 post = {
  content: 'content',
  creator: {
   name: 'Jones',
   id: 1,
   avatar: 'jones.png'
  }
}

EDIT

You can declare type to object post:Post={...}

Then declare classes:

export class Post  {
 content:string;
 creator:Creator
}
export class Creator  {
  name: string;
  id: number;
  avatar: string;
}

Working code

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need to specify post: any. The problem is that OP did not set post equal to the object (post = { content... }).
@לבני מלכה but what if I create object by constructor? post = new Post( { ... })?
not neede new only :Post : post:Post={} :stackblitz.com/edit/hello-angular-6-fmadfp?file=src/app/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.