Slimshadddyyy Slimshadddyyy - 3 months ago 16
AngularJS Question

AngularJS - Call function in service from the same service

HTML

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


Ctrl

$scope.doPlayOrPause = function(){
cameraModuleService.doPlayOrPause();
}


Service

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


Here I am calling service
getVLC
, but it says getVLC is not defined. How do I call one service from the same service ?

P.S. Both functions are in the same service

Answer

If these two functions are in the same file, you have to save the reference on this like this:

var self = this;
this.doPlayOrPause = function(){
    var vlc = self.getVLC("vlc");
    ...
}

UPDATE

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