1

I have an angular service

app.factory('MyService', function($http,$q)){
    return{
        super : function(){
             return "Angular is awesome";
        }
    };
});

and a complex javascript object defined outside Angular's scope (controller or service):

var MyObject = {
    anyFunction : function(){
        alert(MyService.super());
    }
};

Now, doing

MyObject.anyFunction();

of course fails. Any idea how I can get MyService injected into my external javascript object?

zs2020
  • 53,766
  • 29
  • 154
  • 219
Ant
  • 1,812
  • 5
  • 22
  • 39

1 Answers1

1

This is bad way, byt you may save your angular's var to the localStorage and then acces it from legacy code, and as a second option, you may access global variable from factory, using $window service.

In common js:

var myVar;

In factory:

app.factory('MyService', ['$window', '$http', '$q',function($window, $http, $q)){
    return{
        super : function(){
            $window.myVar = 'that is it';
            //or 
            //localStorage.addItem('myVar', 'varValue');
        }
    };
}]);

after MyService.super is done, you may access var in common js as usualy before

S Panfilov
  • 16,641
  • 17
  • 74
  • 96