Mukund Kumar
Mukund Kumar

Reputation: 23211

how to get instance of angular service in javascript code?

i have an angular service. this is the code :

angular.module('myServiceModule', [])
    .service('angService',function({
     this.get=function(){
        console.log('i am accessible');
     }
});

i have to access get() method of angular service in javascript code(not from angular code).

<script>
  function caller(){
    //here i have to access service get() method.how is it possible.
    //can i do this->
    myServiceModule.get();
  }
 caller();//this function i am calling after service load.
</script>

i have done this. but it is not right. How is it possible ?

Upvotes: 3

Views: 668

Answers (1)

Mosho
Mosho

Reputation: 7078

Try this:

angular.injector(['ng', 'myServiceModule']).invoke(["myService", function(myService) {
    //do something with myService
}]);

Upvotes: 3

Related Questions