如何在 Angular2中绑定原始 html

我使用 Angular 2.0.0-beta.0,我想直接创建和绑定一些简单的 HTML?

我试过用

{{myField}}

但是 myField 中的文本会被转义。

对于 Angular 1.x,我找到了对 ng-bind-html 的命中,但是在2. x 中似乎不支持这个命中

弗兰克

106740 次浏览

Bind to the innerHTML attribute

There is 2 way to achieve:

<div [innerHTML]="myField"></div>
<div innerHTML="\{\{myField}}"></div>

To mark the passed HTML as trusted so that Angulars DOM sanitizer doesn't strip parts of

<div [innerHTML]="myField | safeHtml"></div>

with a pipe like

@Pipe({name: 'safeHtml'})
export class Safe {
constructor(private sanitizer:DomSanitizer){}


transform(value: any, args?: any): any {
return this.sanitizer.bypassSecurityTrustHtml(value);
// return this.sanitizer.bypassSecurityTrustStyle(style);
// return this.sanitizer.bypassSecurityTrustXxx(style); - see docs
}
}

See also In RC.1 some styles can't be added using binding syntax