有一个字符串类型的变量,里面包含一个iframe标签。我需要 Angular 将字符串识别为 iframe HTML 标记并填充 HTML。
目前,angular 正在构建它的 has 字符串。
请检查下面的代码
变量:
this.longDesc = "<iframe width="something" height="something" src="something" ></iframe>"
html文件
<div class="description-panel" [innerHTML]="longDesc" ></div>
最佳答案
Step1 : 在ts文件中导入以下部分
import {DomSanitizer} from '@angular/platform-browser';
第二步:在特定的ts文件中定义构造函数
constructor(private domSanitizer:DomSanitizer) {}
第 3 步:- 根据需要(方法/函数或构造函数)在 ts 文件中使用以下代码
this._EmittersService.longDescemitted$.subscribe(obj => {
this.longDesc = this.domSanitizer.bypassSecurityTrustHtml(obj or any html content);
or
this.longDesc = this.domSanitizer.bypassSecurityTrustHtml('<iframe width="something" height="something" src="something" ></iframe>')
});
关于javascript - 当字符串是 Angular Iframe 标记时如何使用 innerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46172057/