Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS-从同一服务调用服务中的函数_Angularjs_Vlc_Angularjs Service - Fatal编程技术网

AngularJS-从同一服务调用服务中的函数

AngularJS-从同一服务调用服务中的函数,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

HTML

<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();
    }
  }
  );