Service functions outside the scope of Angularjs

I created a service in angularJS that uses the btford.socket-io module to interact with the server. Since I have implemented some API in the service that I am currently using inside angular, but for the subsequent extension of the application I also need to provide access to these APIs outside of angular. So in the future, you can simply call the function without having to create a controller and other things.

At the moment, I have done this for the controller

var myController = angular.element($('body')).scope().myController; 

keeping the entire controller inside a variable scope. I was wondering if you can do the same with the service.

+6
source share
1 answer

What about:

angular.element(document.body).injector().get('MyService');

This is generally not a good practice. But sometimes it is necessary.

Note: document.body is an element. Angular app installed on

Another thing you can consider is to "close" the external api using angular through the factory.

  • eg. return your global or name class or api from angular factory.

This is essentially what angular does for you. But instead of creating the link inside angular first and extracting it, you should create it outside and register it with DI as a factory or value.

+11
source

All Articles