Angular 文本插值在ng模板内不起作用
我试图通过文本插值在我的ng模板中传递url地址,但它不起作用 我的html:Angular 文本插值在ng模板内不起作用,angular,Angular,我试图通过文本插值在我的ng模板中传递url地址,但它不起作用 我的html: <ng-template #div2> <iframe width="560" height="315" src="{{video}}" frameborder="0" webkitallowfullscreen allowfullscreen mozallo
<ng-template #div2>
<iframe
width="560"
height="315"
src="{{video}}"
frameborder="0"
webkitallowfullscreen
allowfullscreen
mozallowfullscreen>
</iframe>
</ng-template>
如何修复它以使动态传递视频url成为可能?它不必是文本插值,我只是不想让url硬编码在内部您可以使用let定义参数
<ng-template #template let-video="https://www.youtube.com/embed/BNcxTNrtRdk">
<iframe
width="560"
height="315"
src="{{video}}"
frameborder="0"
webkitallowfullscreen
allowfullscreen
mozallowfullscreen>
</iframe>
</ng-template>
或根据上下文:
<div *ngFor='let video of videos'>
<ng-container
[ngTemplateOutlet]="template"
[ngTemplateOutletContext]="{video:video}">
</ng-container>
</div>
<ng-template #template let-video="{{video}}">
<iframe
width="560"
height="315"
src="{{video}}"
frameborder="0"
webkitallowfullscreen
allowfullscreen
mozallowfullscreen>
</iframe>
</ng-template>
如果有人需要,我已经找到了解决方案。 在ts文件中:
constructor(private sanitizer: DomSanitizer) {}
safeUrl;
ngOnInit() {
this.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl('https://www.youtube.com/embed/BNcxTNrtRdk');
}
然后在html中:
<iframe
[src]='safeUrl'
frameborder="0"
webkitallowfullscreen
allowfullscreen
mozallowfullscreen>
</iframe>
就这样!准备好出发了。不幸的是,这对我不起作用。我得到了未保存的url错误或预期的“{”标记(不知道为什么),但我找到了其他解决方案-我已将其粘贴到另一个注释中
constructor(private sanitizer: DomSanitizer) {}
safeUrl;
ngOnInit() {
this.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl('https://www.youtube.com/embed/BNcxTNrtRdk');
}
<iframe
[src]='safeUrl'
frameborder="0"
webkitallowfullscreen
allowfullscreen
mozallowfullscreen>
</iframe>