簡體   English   中英

當字符串是 angular 的 iframe 標簽時如何使用innerHTML

[英]how to use innerHTML when the string is an Iframe tag in angular

有一個字符串類型變量,其中包含一個 iframe 標記。 我需要 angular 將字符串識別為 iframe HTML 標記並填充 HTML。

目前,angular 正在構建它的字符串。

請檢查以下代碼

變量:

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

html文件

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

步驟1:在ts文件中導入以下部分

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

第 2 步:在特定的 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>')
     });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM