I'm currently working on a single-page application using Angular. I encountered an issue where, when the user presses the browser's back button, only the YouTube iframe content updates and not the entire page. This results in having to press the back button twice to navigate back to the previous page completely. The URL is also not updated on the first press of the back button. This behavior occurs only when navigating through two links with the same route that involve the YouTube iframe element. Deleting history elements is not an option as I need to maintain the history navigation.
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>