使用插值渲染模板中的html字符串? [英] Render html string within a template using interpolation?

查看:308
本文介绍了使用插值渲染模板中的html字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Angular 4.3.0

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>

因此,我跟随文档以及
https://angular.io/guide/template-syntax# !#property-binding-or-interpolation-

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>

错误:

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>

推荐答案

您需要将[innerHTML]作为字符串传递,并用单引号引起来, <div [innerHTML]="'<b>test here</b>'"></div>

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

这篇关于使用插值渲染模板中的html字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆