12

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!.

3
  • 3
    I really think you need to read the quick start tutorial if you are asking a question like this: angular.io/guide/quickstart Commented Sep 25, 2017 at 8:42
  • blog.thoughtram.io/angular/2016/10/13/… Commented Sep 25, 2017 at 8:43
  • I will add that it's bad practice to use capitalized names for variables. Commented Sep 25, 2017 at 9:18

2 Answers 2

13

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';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Step 1:-At html file (profile.component.html)

<div class="col col-sm-12">
  <h2 *ngIf="data==null; else elseBlock" >FirstName LastName</h2>
<ng-template #elseBlock><h2  [innerHTML] = "data"></h2></ng-template>
</div>

Step 2: At ts file (profile.component.ts)

public data:any;
changeName():void{
       this.data="Your Data";
    }

Comments

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.