AngularJS - 来自同一服务的服务中的调用功能

时间:2016-08-05 12:17:45

标签: angularjs vlc angularjs-service

HTML

<input type="button" id="PlayOrPause" style="width:60px" value="Play" onClick='doPlayOrPause();'>

控制

$scope.doPlayOrPause = function(){
 cameraModuleService.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。这两个功能都在同一个服务中

1 个答案:

答案 0 :(得分:1)

如果这两个函数在同一个文件中,则必须将引用保存在this上,如下所示:

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