Javascript 包装YouTube iFrame API的Angular服务-loadVideoById()引发异常
Cdoe: 调用“setVideo”后出现的错误如下:Javascript 包装YouTube iFrame API的Angular服务-loadVideoById()引发异常,javascript,angularjs,iframe,youtube-api,youtube-iframe-api,Javascript,Angularjs,Iframe,Youtube Api,Youtube Iframe Api,Cdoe: 调用“setVideo”后出现的错误如下: TypeError:对象#没有方法“loadVideoById” 在Object.ytplayer.setVideo(http://localhost:8000/js/youtube.js:36:23) at Object.fn(http://localhost:8000/js/controllers.js:2939:33) 在h.$get.h.$digest(http://localhost:8000/lib/angular/1.2.5/
TypeError:对象#没有方法“loadVideoById”
在Object.ytplayer.setVideo(http://localhost:8000/js/youtube.js:36:23)
at Object.fn(http://localhost:8000/js/controllers.js:2939:33)
在h.$get.h.$digest(http://localhost:8000/lib/angular/1.2.5/angular.min.js:98:396)
在h.$get.h.$apply(http://localhost:8000/lib/angular/1.2.5/angular.min.js:101:157)
在HTMLInputElement.h(http://localhost:8000/lib/angular/1.2.5/angular.min.js:123:493)
在HTMLInputElement.x.event.dispatch(http://localhost:8000/js/jquery.min.js:5:14129)
在HTMLInputElement.x.event.add.v.handle(http://localhost:8000/js/jquery.min.js:5:10866)
注销该对象会使我:
Y{b:wb,a:iframe#ytPlayer,o:null,closure_uid_613255056:2,u:2…}
D:反对
a:我是一名球员
b:wb
结案日期(613255056):2
d:43
g:N
h:“玩家”
i:反对
k:反对
o:空
s:数组[0]
t:是的
u:2
__原型:c
非常感谢您的帮助 您的日志数据表明YT播放器尚未初始化。在代码中的什么地方调用loadPlayer函数?如果您可以提供它可能有助于调试。我将发布它,感谢您的评论,也许问题是我在1个控制器中初始化它,并尝试从另一个控制器调用loadVideoById?
(function(){
angular.module('youtubeAPI', []).
run(function() {
var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
}).
service('YtPlayerApi', ['$window', '$rootScope', function ($window, $rootScope) {
var ytplayer = {"playerId":null,
"playerObj":null,
"videoId":null,
"height":'100%',
"width":'100%'};
$window.onYouTubeIframeAPIReady = function () {
this.APIloaded = true;
};
ytplayer.readyState = function(){
return this.APIloaded;
}
ytplayer.setVideoID = function(videoId){
this.videoId = videoId;
},
ytplayer.getVideoId = function(){
return this.videoId;
},
ytplayer.getApi = function(){
return this;
},
ytplayer.setPlayerId = function(elemId) {
this.playerId=elemId;
},
ytplayer.setVideo = function(videoId) {
console.log( this.playerObj);
this.playerObj.loadVideoById(videoId);
},
ytplayer.loadPlayer = function () {
this.playerObj = new YT.Player(this.playerId, {
height: this.height,
width: this.width,
videoId: this.videoId
});
this.APIloaded = true;
}
return ytplayer;
}]);
})()