2015-10-20 64 views
0

我有一个angularjs应用程序,我想在新闻源中显示一些youtube视频。 我已经创建了一个指令来加载播放器,并且在我第一次进入包含视频的视图时加载了我的视频。但是,如果我离开视图,并再次打开我的视频不加载。onYoutubeIFrameAPIReady只开了一次

看来,事件“onYouTubeIframeAPIReady”仅在我第一次进入视图时触发。

的视图:

 <div bind-html-compile="newsitem.description"></div> 

的newsitem.description是原始HTML发送给应用程序,JSON。

所以它可能看起来像:

{'this is a video <youtube videoid="myYoutubeVideoId"></youtube>'} 

这也是为什么使用IM绑定,HTML编译指令。

该指令

.directive('youtube', function($window) { 
return { 
    restrict: "E", 

    scope: { 
     videoid: "@" 
    }, 

    template: '<div></div>', 

    link: function(scope, element, attrs) { 
     var tag = document.createElement('script'); 
     tag.src = "https://www.youtube.com/iframe_api"; 
     var firstScriptTag = document.getElementsByTagName('script')[0]; 
     firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

     var player; 

     $window.onYouTubeIframeAPIReady = function() { 
      player = new YT.Player(element.children()[0], { 
       playerVars: { 
        autoplay: 0, 
        html5: 1, 
        theme: "light", 
        modesbranding: 1, 
        color: "white", 
        iv_load_policy: 3, 
        showinfo: 0, 
        controls: 0, 
        rel: 0, 
       }, 
       videoId: scope.videoid 
      }); 
     }; 
    } 
}}) 

,当我从onYoutubeIFrameAPIReady CONSOLE.LOG我只得到输出的第一次.. 所以即时通讯思想,该事件被触发不知何故之前的功能已准备就绪,但我可以真的不知道。

任何想法?

回答

3

我遇到同样的问题。看完这篇文章,其中我假设你跟着,因为你的代码是非常接近文章的代码,你刚才没看文章的底部:

http://blog.oxrud.com/posts/creating-youtube-directive/

我发现在底部的为什么会发生这种情况的解释以及演示多个视频工作的演示。我没有试过但要自己解决,但要回答你的问题:

http://run.plnkr.co/plunks/8lxuN8/

+0

证实了这一工作。现在使用工厂,我可以看到我所有的视频都在播放。 – Squiggler

+0

Excactly我需要..谢谢! – OngoBoingo