【发布时间】:2017-02-18 22:25:00
【问题描述】:
我正在尝试使用 innerHTML 和从 SQL 获得的 html + css 字符串呈现 HTML 模板。
模板字符串示例:
<html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Template Name</title><style type="text/css"> p{ color:red; }</style> </head> <body> <h1>#headding#</h1> <p style="color:red;">#paragraph#</p><a href="#url#">#urltext#</a> </body> </html>
现在它可以很好地呈现 HTML,但看起来它会丢弃样式标签并只呈现其中的文本。
渲染示例:
HTML 渲染部分:
<div [innerHtml]="templateBody">
</div>
Home.component.ts 部分:
@Component({
selector: "home",
templateUrl: `client/modules/home/home.component.html`,
encapsulation: ViewEncapsulation.Emulated
})
export class HomeComponent implements OnInit{
templateBody: string;
.....other code
}
我已经尝试过封装:ViewEncapsulation.Emulated/None 等,尝试了内联 CSS,我尝试在 p 标签前附加 :host >>>。它们都呈现相同的效果。
有什么建议吗?
【问题讨论】: