-1

i have a problem with asynchronous loading data from server. In view are no any data because rendering is before load. Here is my code: app.component.ts:

    user: User = new User()
    ngOnInit() {
    this.userService.getUser(1).then(usr => {
      this.user = usr;
    }).catch((error: any) => {

    });
  }

app.component.html:

<p>{{user.name}}</p>

I thought that the data would change dynamically. Where is problem? Thanks

6
  • Can you try <p>{{user?.name}}</p>? Commented May 29, 2017 at 12:00
  • Yes, result is same. I dont see user name Commented May 29, 2017 at 12:01
  • Try to log the this.user after assignment in async call and show us the content Commented May 29, 2017 at 12:06
  • Did you change the DetectionStrategy in this\parent component? Commented May 29, 2017 at 12:07
  • 2
    @jonrsharpe, just wanted to point that was not exactly the issue here as in dupe suggestion. If you check the comment in below answer by OP. Data structure was changed, so apparently user no longer had property name ;) Commented May 29, 2017 at 12:18

2 Answers 2

1

Try and use the elvis operator this might help

<p>{{user?.name}}</p>

Or may even try to make use of ngIf

<div *ngIf = "user">
    user.name
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

This is not good solution. I dont see user name. I created service for store user and getter and setter method a i put breakpoint to get set, first call get method and than set method
is the user getting logged in the component check that ?
I am sorry, problem is in data which i get from server. Data structure has been changed and i dont know. It works now
@bluray then why have you accepted this answer? It doesn't solve the problem.
0
<p>{{user.name}}</p>

OR

<div *ngIf="user">
    {{user.name}}
</div>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.