1. 程式人生 > >angular/ionic中對img、iframe等的src進行動態繫結變數的問題解決

angular/ionic中對img、iframe等的src進行動態繫結變數的問題解決

例如以下程式碼: 對應的html頁面中是這樣:<iframe class=“filling” [src] = “iframe”> 在這裡直接對src進行動態繫結變數就會出現:unsafe value used in a resource URL context (see http://g.co/ng/security#xss)問題。 檢視官方文件會發現angular在這方面做了關於防止xss攻擊的安全機制,以上的做法是不符合要求的。 解決辦法: 1.在對應ts檔案中,匯入DomSanitizer,SafeResourceUrl import {DomSanitizer, SafeResourceUrl} from ‘@angular/platform-browser’; 2、引用DomSanitizer,SafeResourceUr pageUrl:string; iframe: SafeResourceUrl; constructor(public domSanitizer:DomSanitizer,public commonConfig: CommonConfig ) { this.pageUrl = this.commonConfig.activitUrl sessionStorage.getItem(‘property’); this.iframe = this.domSanitizer.bypassSecurityTrustResourceUrl( this.pageUrl); }