我有一个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我只得到输出的第一次.. 所以即时通讯思想,该事件被触发不知何故之前的功能已准备就绪,但我可以真的不知道。
任何想法?
证实了这一工作。现在使用工厂,我可以看到我所有的视频都在播放。 – Squiggler
Excactly我需要..谢谢! – OngoBoingo