4

How can I add a html element dynamically using string interpolation without add any html tag? Exp:

This is my obj in my component

myObj = {
    title: 'Header Title',
    icon: '<i class="fa fa-user">'
}

I want to add this like;

<header>
    {{myObj.title}} - {{myObj.icon}}
</header>

{{myObj.icon}} rendered as text, but I want render as html. How can?

I want the result to be as follows

<header>
    Header Title <i class ="fa fa-user"></i>
</header>

and if I change my obj like this;

myObj = {
    title: 'Header Title',
    icon: '<mat-icon>search</mat-icon>'
}

render as

<header>
    Header Title <mat-icon>search</mat-icon>
</header>
midstack
  • 2,105
  • 7
  • 44
  • 73

1 Answers1

6

You can leverage innerHTML

<header>
    <span>{{myObj.title}}</span> - <span [innerHTML]="myObj.icon"></span>
</header>

Note : I have used to segregated the elements however you can pick any element as per your design.

Sunil Singh
  • 11,001
  • 2
  • 27
  • 48