3
I’m developing an Angular 2 application and I’m using the Youtube API. The Youtube API requires me to implement some functions in the global scope, so I did the following:
export class MyClass {
dados: any;
constructor( ... ) {
...
}
loadAPI(){
(window as any).onYouTubeIframeAPIReady = function () {
buildPlayer();
}
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
console.log('API loaded');
}
}
function buildPlayer(){
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
console.log('youtube iframe api ready!');
}
function onPlayerReady(event){
// AQUI ESTÁ O PROBLEMA
// Eu quero manipular "dados" dentro dessas funções.
}
function onPlayerStateChange(status){
// AQUI ESTÁ O PROBLEMA
// Eu quero manipular "dados" dentro dessas funções.
}
The code loads the API correctly and creates the player, but I cannot manipulate the data of the variable that is within the class in these functions. Any idea how I can fix this?
Which variable you need to change?
– Jéf Bueno
I put that variable "data" just as an example, let’s say that it has data coming from the database, I need that data to be passed to these functions below.
– thsnj
You’re creating the functions outside the class, so you can’t access.
– Jéf Bueno