Angularjs自定义指令编译函数未被调用

时间:2015-04-15 17:41:07

标签: javascript angularjs youtube-javascript-api

我正在创建一个angularjs指令来渲染youtube video.using iframe Api。 我在编译函数中调用api。 但是编译功能本身并没有被调用。 这是掠夺者  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;
})

我做错了什么?

2 个答案:

答案 0 :(得分:0)

错字。您错误拼写compilecomplie。 你现在可以继续踢自己:P

答案 1 :(得分:0)

这将Plunkr为您解决问题吗?

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