AngularJS-从同一服务调用服务中的函数
HTMLAngularJS-从同一服务调用服务中的函数,angularjs,vlc,angularjs-service,Angularjs,Vlc,Angularjs Service,HTML <input type="button" id="PlayOrPause" style="width:60px" value="Play" onClick='doPlayOrPause();'> 服务 cameraModuleService.service('cameraModuleService', function ($http, $timeout, ENV, $log, $rootScope, $cookieStore, $compile) { thi
<input type="button" id="PlayOrPause" style="width:60px" value="Play" onClick='doPlayOrPause();'>
服务
cameraModuleService.service('cameraModuleService', function ($http, $timeout, ENV, $log, $rootScope, $cookieStore, $compile) {
this.getVLC = function(){
if( window.document[name] ){
return window.document[name];
}
if( navigator.appName.indexOf("Microsoft Internet") == -1 ){
if( document.embeds && document.embeds[name] )
return document.embeds[name];
}else{
return document.getElementById(name);
}
}
this.doPlayOrPause = function(){
var vlc = getVLC("vlc");
if( vlc )
{
if( vlc.playlist.isPlaying )
vlc.playlist.togglePause();
else
vlc.playlist.play();
}
}
);
这里我调用服务getVLC
,但它说getVLC没有定义。如何从同一服务中调用一个服务
p.S.两个功能都在同一个服务中如果这两个功能在同一个文件中,您必须将引用保存在
上,如下所示:
var self = this;
this.doPlayOrPause = function(){
var vlc = self.getVLC("vlc");
...
}
更新
cameraModuleService.service('cameraModuleService', function ($http, $timeout, ENV, $log, $rootScope, $cookieStore, $compile, $window) {
this.getVLC = function () {
if ($window.document[name]) {
return $window.document[name];
}
if (navigator.appName.indexOf("Microsoft Internet") == -1) {
if (document.embeds && document.embeds[name])
return document.embeds[name];
} else {
return document.getElementById(name);
}
}
var self = this;
this.doPlayOrPause = function () {
var vlc = self.getVLC("vlc");
if (vlc) {
if (vlc.playlist.isPlaying)
vlc.playlist.togglePause();
else
vlc.playlist.play();
}
}
);
这两个函数在同一个文件中?请提供完整的服务代码。更新后的问题可能重复。是的,这两个函数都在同一个服务中。为什么我不能使用this.getVLC
调用它,而是定义var self
?也使用它不返回任何内容。因为this
引用函数范围(当您在函数中调用它时),并且getVLC
未在函数范围中定义,但是在您的服务范围内,我应该做哪些更改才能使其正常工作?我在我的服务中做了相同的更改,但它仍然显示未定义
。它是否会在service.js中获得window.document
?我认为您必须在依赖项中包含$window
,并通过$window
更改window
(c.f更新)
cameraModuleService.service('cameraModuleService', function ($http, $timeout, ENV, $log, $rootScope, $cookieStore, $compile, $window) {
this.getVLC = function () {
if ($window.document[name]) {
return $window.document[name];
}
if (navigator.appName.indexOf("Microsoft Internet") == -1) {
if (document.embeds && document.embeds[name])
return document.embeds[name];
} else {
return document.getElementById(name);
}
}
var self = this;
this.doPlayOrPause = function () {
var vlc = self.getVLC("vlc");
if (vlc) {
if (vlc.playlist.isPlaying)
vlc.playlist.togglePause();
else
vlc.playlist.play();
}
}
);