0

I am trying to get the nested object to display in the template with a get request. The issue is it display the main properties of my object, though, the nested object.

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

This is my model which can show user properties but not 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;
    });
}

In my html

{{users.firstname}} // work

{{users.information.adress}} // Does not work
1
  • Can you do console.log(this.users) after the line this.users = data; and please check how the object looks like in the console Commented Sep 22, 2020 at 2:31

1 Answer 1

2

I see a couple of issues. In the User class, you misspelled one of the properties as inforamation, but in the template, the property is spelled correctly as information.

If you have an array of Users that you want to render, you can use Angular's *ngFor to iterate through them, as shown below:

<div *ngFor="let user of users">
  {{user.firstName}}
  {{user.information.address}}
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Woow I didn't expected that. It was the misspelled property "Inforamation" . thx a lot
No problem! Glad that helped. This kind of error is usually logged in the console of your browser. If you're using Chrome, you can open the console by right-clicking anywhere on the webpage and clicking the "Inspect" option, then navigating to the "Console".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.