为此你需要一个管道并使用 DomSanitizer
例如
import { MyApp, SafePipe } from './myapp.component';
@NgModule({
declarations: [
AppComponent,
SafePipe
],
在您的应用中创建管道
import { Component, ViewEncapsulation, ViewChild, ElementRef, PipeTransform, Pipe, OnInit } from '@angular/core';
import { DomSanitizer } from "@angular/platform-browser";
@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) { }
transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
@Component({
selector: 'app-root',
templateUrl: './myapp.component.html',
styleUrls: ['./myapp.component.css']
})
export class MyApp {
title = 'app';
myiframe: string = "your_url_here"
}
在你的 html 中
<iframe width="420" height="345" [src]="myiframe | safe">
</iframe>