I'm developing a SPA using angular. When the history back button is pressed, the browser changes only the Youtube iframe and not the entire page, I need to press 2 times the back button to go in the full previous page (at the first time the URL is not updated). This happens only when following 2 links with the same route involving the YT iframe element. I need to keep history navigation, so I can't only delete history elements
Component
export class PlayerComponent implements OnInit {
videoName: string;
videoUrl: any;
constructor(
private sanitizer: DomSanitizer,
private route: ActivatedRoute
) {}
ngOnInit() {
this.route.params.subscribe( params => {
this.videoUrl = this.sanitizer.bypassSecurityTrustResourceUrl('https://www.youtube.com/embed/' + params.videoId);
});
}
}
HTML
<div class="embed-responsive embed-responsive-16by9">
<iframe class="embed-responsive-item border border-primary" *ngIf="videoUrl"
[src] = "videoUrl"
allowfullscreen>
</iframe>
</div>
You don't need to set "src" in the subscribe, you need to replace url:
.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
ngOnInit() {
this.route.params.subscribe( params => {
this.iframe.nativeElement.contentWindow.location.replace('https://www.youtube.com/embed/' + params.videoId);
});
}
@ViewChild("iframe") iframe: ElementRef;
.html
<iframe #iframe class="embed-responsive-item border border-primary" *ngIf="videoUrl"
src="about:blank"
allowfullscreen>
</iframe>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With