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?

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.