欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

angular中如何綁定iframe中src的方法

 更新時(shí)間:2019年02月01日 09:47:21   作者:YellRes  
這篇文章主要介紹了angular中如何綁定iframe中src的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

需求: 頁面中有一個(gè)網(wǎng)頁組件(由iframe編寫),此iframe顯示在一個(gè)輸入框中,當(dāng)修改輸入框中地址的時(shí)候,要求改變網(wǎng)頁組件中的內(nèi)容

網(wǎng)頁組件中的代碼(html的部分)

 <iframe
  #Iframe
  [src]="testUrl"
  frameborder="0"
  width="100%"
  height="100%">
 </iframe>

網(wǎng)頁組件中的代碼(ts的部分)

...省略
export class DesignWebInputComponent implements OnInit{
  testUrl ;
}

此時(shí)問題出現(xiàn)了,頁面無法顯示內(nèi)容

不要慌,有辦法可以解決

constructor( private sanitizer:DomSanitizer) {}

導(dǎo)入DomSanitizer 這個(gè)類 并使用其中的bypassSecurityTrustResourceUrl() 轉(zhuǎn)換url的格式 如下

 trustUrl(url: string) {
  if(url){
   return this.sanitizer.bypassSecurityTrustResourceUrl(url);
  }
 }

html中

 <iframe
  #Iframe
  [src]="trustUrl(testUrl)"
  frameborder="0"
  width="100%"
  height="100%">
 </iframe>

在這里寫了個(gè)trustUrl()轉(zhuǎn)換 testUrl 這樣就可以顯示了

總結(jié): 使用 DomSanitizer 類中的 bypassSecurityTrustResourceUrl() 來轉(zhuǎn)換url

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論