0

I am creating a angularjs directive to render youtube video.using Iframe Api. I am calling the api in the compile function. But the compile function itsely is not getting called. here is the plunker http://plnkr.co/edit/PcpzOoYq73pKGeB7nZP5?p=preview

   TimelyApp.directive('youtube', function($window) {
    var directive = {};
    var player;

    directive.restrict = 'E';

    directive.template = '<div id="myPlayer"></div>';

    directive.complie = function(element, attribute) {
        console.log("compile working");
        var scriptTag = document.createElement("script");
        scriptTag.src = "http://www.youtube.com/iframe_api";
        var orignalScriptTag = document.getElementsByTagName('script')[0];
        console.log(orignalScriptTag.parentNode);
        orignalScriptTag.parentNode.insertBefore(scriptTag, orignalScriptTag);
        var link = function($scope, element, attribute) {
            $window.onYouTubeIframeAPIReady = function() {
                player = new YT.Player('myPlayer', {
                    height: '390',
                    width: '670',
                    events: {
                        'onReady': onPlayerReady,
                    }
                });
            };
        };

        return link;
    };

    var onPlayerReady = function(event){
        console.log(event);
                            player.cuePlaylist(["OG0xt2xTq4A","jOYR3k1VhUQ"]);
                        //event.target.playVideo();
                            player.playVideo();

    };

    return directive;
})

What am I doing wrong?

marcio
  • 10,002
  • 11
  • 54
  • 83
Atishay Baid
  • 339
  • 2
  • 7

2 Answers2

0

Typo. You mis-spelled compile as complie. You can go ahead and kick yourself now :P

HankScorpio
  • 3,612
  • 15
  • 27
0

will this Plunkr Solve the issue for you?

app.directive('myYoutube', function($sce) {
  return {
    restrict: 'EA',
    scope: { code:'=' },
    replace: true,
    template: '<div style="height:400px;"><iframe style="overflow:hidden;height:100%;width:100%" width="100%" height="100%" src="{{url}}" frameborder="0" allowfullscreen></iframe></div>',
    link: function (scope) {
        console.log('here');
        scope.$watch('code', function (newVal) {
           if (newVal) {
               scope.url = $sce.trustAsResourceUrl("http://www.youtube.com/embed/" + newVal);
           }
        });
    }
  };
});

Create a YouTube AngularJS Directive

Community
  • 1
  • 1
Matju
  • 51
  • 6