Angular 10:使用变量设置 IFrame URL 输入

3 iframe typescript angular angular10

我正在尝试使用下面的组件设置 IFrame 源输入。

由于某种原因它不起作用,没有显示任何网页。如何正确设置输入源?

调用方法:

<app-viewer-iframe-two
    [url1]="'http://www.food.com'"
    [url2]="'http://www.car.com'"
>
</app-viewer-iframe-two>
Run Code Online (Sandbox Code Playgroud)

打字稿:

export class ViewerIframeTwoComponent implements OnInit {

  constructor() { }

  @Input() url1: string;
  @Input() url2: string;
  
  ngOnInit(): void {
  }
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="box">
  <iframe class="iframe-one" src="{{url1}}" frameborder="0"
    scrolling="yes" align="left"> </iframe>
</div>

<div class="box">
  <iframe class="iframe-two" src="{{url1}}" frameborder="0"
    scrolling="yes" align="right"></iframe>
</div>
Run Code Online (Sandbox Code Playgroud)

错误:

没有设置任何来源,

在此输入图像描述

资源:这仅适用于 Angular 1

如何从 AngularJS 中的变量设置 iframe src 属性

its*_*ers 5

是的,将任何内容放入与 Angular.js 中相同的 url 中仍然是不安全的。只需绕过它 - 它仍然不安全,但您将代替角度开发人员承担责任。

  constructor(public sanitizer: DomSanitizer) { }

  ngOnInit() {
    this.urlSafe= this.sanitizer.bypassSecurityTrustResourceUrl(this.url);
  }
Run Code Online (Sandbox Code Playgroud)

写为[src]="urlSafe"

https://angular.io/api/platform-b​​rowser/DomSanitizer