Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render html string within a template using interpolation?

Tags:

angular

Using Angular 4.3.0

Say I have a string like this that is a property of a component.

<p>test#2 bla bla</p><p>test1234 56</p><p>test test</p>

So, Im following along with the docs,
https://angular.io/guide/template-syntax#!#property-binding-or-interpolation-

This does not work:
What am I doing wrong?

<div class="panel-group">
    <div class="panel panel-default" *ngFor="let b of BlogPostList">
        <div class="panel-heading">{{b.Title}}</div>
        <div class="panel-body">
            <span style="text-align:left">Created:{{b.DateCreated | date: 'MM/dd/yyyy'}}</span><span style="text-align:right">Last updated{{b.DateUpdated | date: 'MM/dd/yyyy'}}</span>
            <br/>
            <div [innerHTML]="<b>test here</b>"></div>
        </div>
    </div>
</div>

error:

Unhandled Promise rejection: Template parse errors:
Parser Error: Unexpected token < at column 1 in [<b>test here</b>] in ...</span>
                <br/>
                <div [ERROR ->][innerHTML]="<b>test here</b>"></div>
            </div>
        </div>
like image 929
bitshift Avatar asked Oct 12 '17 14:10

bitshift


2 Answers

You need to pass your [innerHTML] as a string wrap it with single quotes, <div [innerHTML]="'<b>test here</b>'"></div>

like image 137
penleychan Avatar answered Oct 08 '22 12:10

penleychan


You shoud write the parameter as a string.

 <div [innerHTML]="'test here'"></div>
like image 44
omeralper Avatar answered Oct 08 '22 13:10

omeralper