Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Change text of HTML element

I want to change the text of a html element.

profile.component.html

<div class="col col-sm-12">
  <h2>FirstName LastName</h2>
</div>

profile.component.ts

changeName():void{
        //Code to change the <h2> element
    }

If you could provide code example how to do this, it would be good!.

like image 764
Amar Avatar asked Sep 25 '17 08:09

Amar


1 Answers

Use interpolation using the double curly braces {{ }} and bind your FirstName and LastName. Read more about template syntax.

Change your html to following:

<div class="col col-sm-12">
  <h2>{{ FirstName }} {{ LastName }}</h2>
</div>

... and in your profile.component.ts:

FirstName: string = '';
LastName: string = '';

changeName():void{
    this.FirstName = 'New First Name';
    this.LastName = 'New Last Name';
}
like image 75
Faisal Avatar answered Sep 24 '22 13:09

Faisal