当字符串是angular中的Iframe广告代码时,如何使用innerHTML [英] how to use innerHTML when the string is an Iframe tag in angular

查看:56
本文介绍了当字符串是angular中的Iframe广告代码时,如何使用innerHTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个字符串类型变量,其中包含一个iframe标记.我需要用角度将字符串标识为iframe HTML标签并填充HTML.

There is a string type variable, which contains an iframe tag. I need angular to identify the string as an iframe HTML tag and populate the HTML.

当前,angular正在构造其具有字符串的

Currently, angular is constructing its has string.

请检查以下代码

变量:

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

html文件

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

推荐答案

第一步:将以下部分导入ts文件

Step1 : Import the below part in ts file

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

第2步:在垂直ts文件中定义构造函数

Step 2 :Define constructor in perticular ts file

constructor(private domSanitizer:DomSanitizer) {}

第3步:-根据需要(方法/函数或构造函数)在ts文件中使用以下代码

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

这篇关于当字符串是angular中的Iframe广告代码时,如何使用innerHTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆