brandly / angular-youtube-embed

:tv: Embed a YouTube player with a simple directive
http://brandly.github.io/angular-youtube-embed/
MIT License
508 stars 147 forks source link

Cannot access player from parent controller #109

Closed labago closed 8 years ago

labago commented 8 years ago

Whenever I try to do something like

$scope.player.setVolume($scope.playerSettings.volume);

I get an undefined error, meaning "player" is not defined. I do have the attribute on the directive set to a name like this:

<youtube-video ng-if="playerSettings.ready" video-id="currentVideo" player-vars="playerSettings" player="thePlayer"></youtube-video>

And doing something like this:

$scope.thePlayer.setVolume($scope.playerSettings.volume);

Gets the same undefined error. I am relatively new to angular but according to what I have read, the first example in this post should have worked right? What am I doing wrong?

UniSoundEcho commented 8 years ago

You have to broadcast player to rootscope. Then create a listener and convert it to local $scope.player

brandly commented 8 years ago

the root of the problem is that ng-if creates a new scope. $scope.player, when used in your controller, is being referenced from within the parent's scope, so it's undefined.

depending on your needs, an ng-show might work, since it doesn't create a new scope.