1

In userdata I have got firstname , lastname.... So I want to bind firstname in homecomponent.html, so how can I bind it in html template ?

export class HomeComponent {

    constructor(private authenticationService : AuthenticationService) {
        userdata=this.authenticationService.user_data;
    }
}
1
  • 1
    What you are getting as userdata have you tried to console it? If yes then what is it? Commented Jun 20, 2017 at 6:08

2 Answers 2

1

Just,

<div><h1>{{userdata.firstname}}</h1></div> 

EDIT:

if your data comes from an asynchrnous call, then use elvis operator to check the value exists,

<div><h1>{{userdata?.firstname}}</h1></div> 
Sign up to request clarification or add additional context in comments.

10 Comments

show error: home.component.html:19 ERROR TypeError: Cannot read property 'firstname' of undefined at Object.eval [as updateRenderer] (home.component.html:22)
try using elvis operator like this <div><h1>{{userdata?.firstname}}</h1></div>
can you please explain me about it brief
yaah sure but its show wait for 5 minute I will mark after 5 minute
what do you want me to explain?
|
0

If you're discussing interpolation you can show any field of the userdata object you can do this in your html:

<p>{{userdata.firstname}} {{userdata.lastname}}</p>

2 Comments

show error: home.component.html:19 ERROR TypeError: Cannot read property 'firstname' of undefined at Object.eval [as updateRenderer] (home.component.html:22)
It depends on what are the fields of your user data. Try console logging userdata after you assign the value to see what the field names are.

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.