我正在做一个教程,涉及iframe src属性的设置:
<iframe width="100%" height="300" src="{{video.url}}"></iframe>
这会抛出一个异常:
Error: unsafe value used in a resource URL context
at DomSanitizationServiceImpl.sanitize...
我已经尝试使用[src]绑定,但没有成功。
我正在做一个教程,涉及iframe src属性的设置:
<iframe width="100%" height="300" src="{{video.url}}"></iframe>
这会抛出一个异常:
Error: unsafe value used in a resource URL context
at DomSanitizationServiceImpl.sanitize...
我已经尝试使用[src]绑定,但没有成功。
当前回答
我通常添加单独的安全管道可重用组件如下
# Add Safe Pipe
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({name: 'mySafe'})
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {
}
public transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
# then create shared pipe module as following
import { NgModule } from '@angular/core';
import { SafePipe } from './safe.pipe';
@NgModule({
declarations: [
SafePipe
],
exports: [
SafePipe
]
})
export class SharedPipesModule {
}
# import shared pipe module in your native module
@NgModule({
declarations: [],
imports: [
SharedPipesModule,
],
})
export class SupportModule {
}
<!-------------------
call your url (`trustedUrl` for me) and add `mySafe` as defined in Safe Pipe
---------------->
<div class="container-fluid" *ngIf="trustedUrl">
<iframe [src]="trustedUrl | mySafe" align="middle" width="100%" height="800" frameborder="0"></iframe>
</div>
其他回答
这个适合我。
import { Component,Input,OnInit} from '@angular/core';
import {DomSanitizer,SafeResourceUrl,} from '@angular/platform-browser';
@Component({
moduleId: module.id,
selector: 'player',
templateUrl: './player.component.html',
styleUrls:['./player.component.scss'],
})
export class PlayerComponent implements OnInit{
@Input()
id:string;
url: SafeResourceUrl;
constructor (public sanitizer:DomSanitizer) {
}
ngOnInit() {
this.url = this.sanitizer.bypassSecurityTrustResourceUrl(this.id);
}
}
我通常添加单独的安全管道可重用组件如下
# Add Safe Pipe
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({name: 'mySafe'})
export class SafePipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {
}
public transform(url) {
return this.sanitizer.bypassSecurityTrustResourceUrl(url);
}
}
# then create shared pipe module as following
import { NgModule } from '@angular/core';
import { SafePipe } from './safe.pipe';
@NgModule({
declarations: [
SafePipe
],
exports: [
SafePipe
]
})
export class SharedPipesModule {
}
# import shared pipe module in your native module
@NgModule({
declarations: [],
imports: [
SharedPipesModule,
],
})
export class SupportModule {
}
<!-------------------
call your url (`trustedUrl` for me) and add `mySafe` as defined in Safe Pipe
---------------->
<div class="container-fluid" *ngIf="trustedUrl">
<iframe [src]="trustedUrl | mySafe" align="middle" width="100%" height="800" frameborder="0"></iframe>
</div>
这对我很有用
我在iframe中定义了一个id
<iframe id="embeddedPage"></iframe>
在组件中,我使用了这段代码
export class YourComponent implements OnInit {
constructor() {}
ngOnInit(): void {
const iframe = document.getElementById('embeddedPage') as HTMLIFrameElement;
iframe.contentWindow.location.replace('your url');
}
}
更新v8
下面回答工作,但暴露您的应用程序的XSS安全风险! 与其使用this. domsaniizer . bypasssecuritytrustresourceurl (url),不如使用this. domsaniizer .sanitize(SecurityContext. sanitize)。URL, URL)
更新
对于RC.6^版本使用DomSanitizer
砰砰作响
一个好的选择是使用纯管道:
import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer} from '@angular/platform-browser';
@Pipe({ name: 'safe' })
export class SafePipe implements PipeTransform {
constructor(private domSanitizer: DomSanitizer) {}
transform(url) {
return this.domSanitizer.bypassSecurityTrustResourceUrl(url);
}
}
记得把你的新SafePipe添加到AppModule的declarations数组中。(详见文件)
@NgModule({
declarations : [
...
SafePipe
],
})
html
<iframe width="100%" height="300" [src]="url | safe"></iframe>
砰砰作响
如果你使用嵌入标签,这可能对你很有趣:
如何使用angular2rc。6禁用消毒嵌入HTML标签显示PDF
旧版RC.5
你可以像这样利用DomSanitizationService:
export class YourComponent {
url: SafeResourceUrl;
constructor(domSanitizationService: DomSanitizationService) {
this.url = domSanitizer.bypassSecurityTrustResourceUrl('your url');
}
}
然后在模板中绑定到url:
<iframe width="100%" height="300" [src]="url"></iframe>
不要忘记添加以下导入:
import { SafeResourceUrl, DomSanitizationService } from '@angular/platform-browser';
砰砰作响的样本
祝贺!¨^ ^ 我有一个简单有效的解决方案给你,是的!
<iframe width="100%" height="300" [attr.src]="video.url"></iframe
[attr。Src]而不是Src ”视频。而不是{{video.url}}
伟大的,)