Angular 4 | iframe to be populated in innerHTML

时间秒杀一切 提交于 2019-12-20 04:46:26

问题


I have a problem

I have subscribed to API which returns some HTML content. sometimes it has iframes. however, only the iframe is not loading and instead it is showing as a string in the browser. Please check below code

Component File

this._EmittersService.longDescemitted$.subscribe(obj => {
  this.longDesc = obj;
 });`

html file

<div class="description-panel" [innerHTML]="longDesc" ></div>

Problem: when the

this.longDesc = "<iframe width="something" height="something" src="something" ></iframe>"

this is displayed as a string, it is not detecting it has an iframe. I NEED the angular to allow it to be rendered as HTML.


回答1:


Step1 : Import the below part in ts file

import {DomSanitizer} from '@angular/platform-browser';

Step 2 :Define constructor in perticular ts file

constructor(private domSanitizer:DomSanitizer) {}

Step 3:-Use below code in ts file according to requirement (method/ function or constructor)

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>')
     });


来源:https://stackoverflow.com/questions/46172057/angular-4-iframe-to-be-populated-in-innerhtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!