我有这样的脚本...
TS:
export class namaClass{
content: any;
constructor(public navCtrl: NavController, public navParams: NavParams) {
this.content = [
{judul: "Judul 1", isi:"<div id='title_1'>Isinya</div>"},
{judul: "Judul 2", isi:"<div id='title_2'>Isinya</div>"},
]
}
}
.html:
<div *ngFor="let item of content">
<h3 [innerHTML]="item.judul"></h3>
<div [innerHTML]="item.isi"></div>
</div>
当我尝试 DOM/inspect 元素时,[innerHTML]="item.isi"
中的元素 ID 不会显示。如何显示该 ID?
这里是 :
组件侧 :
import { DomSanitizer } from '@angular/platform-browser';
constructor(protected _sanitizer: DomSanitizer) {}
safeHtml(html) {
return this._sanitizer.bypassSecurityTrustHtml(html);
}
模板端:
<div *ngFor="let item of content">
<h3 [innerHTML]="safeHtml(item.judul)"></h3>
<div [innerHTML]="safeHtml(item.isi)"></div>
</div>
链接到 : 工作演示
了解更多详情 : https://angular.io/guide/security