angular動態綁定iframe的src報錯unsafe value used in a resource URL context

angular項目中要以變量的形式,外部引入src,結果報錯如下:

<iframe name="myiframe" id="iframe1" [src]="srcUrl" width="100%" height="700px" ></iframe>

解決方式:
1.導入DomSanitizer

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

2.引用DomSanitizer,並轉換url

srcUrl:any;
  constructor(private sanitizer: DomSanitizer) {
    this.srcUrl = this.sanitizer.bypassSecurityTrustResourceUrl(url);

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章