ionic 2 innerHTML缺少属性id

时间:2017-11-15 06:51:02

标签: angular ionic2

我有这样的剧本...

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?

1 个答案:

答案 0 :(得分:1)

这是:

组件方:

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>

链接至:WORKING DEMO

有关详情: https://angular.io/guide/security