To reproduce the problem in Stackblitz, click GO to navigate to the component containing iframe, it works now, then go back and forward, the iframe disappears. You have to refresh the page to show the iframe again.
I tried some workaround :
- I tried to release the object in
ngOnDestroywindow['onYouTubeIframeAPIReady'] = null;but not success
This is the code how to create iframe
init() {
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}
ngOnInit() {
this.init();
window['onYouTubeIframeAPIReady'] = (e) => {
this.YT = window['YT'];
this.player = new window['YT'].Player('player', {
videoId: '1cH2cerUpMQ'
});
};
}
TEMPLATE
<div id="player" >
</div>
Someone has a workaround with success please share, please.