Call angularjs service from simple js code
Asked Answered
C

4

30

I have the following angularjs service:

angular.module('app.main').factory('MyService', ["$http", function ($http) {
    return new function () {

        this.GetName = function () {
            return "MyName";
        };
    };
}]);

How can I call GetName function from MyService from legacy js code?

Clactonian answered 26/6, 2013 at 12:17 Comment(0)
I
45

Use angular.injector. Using your code you can do something like the following:

angular.module('main.app', []).factory('MyService', ['$http', function ($http) {
    return new function () {

        this.GetName = function () {
            return "MyName";
        };
    };
}]);


angular.injector(['ng', 'main.app']).get("MyService").GetName();

Here is the jsfiddle: http://jsfiddle.net/wGeNG/

NOTE - You need to add "ng" as your first module before loading your custom module since your example code depends upon $http provider which is in the ng module.

EDIT - Using get() as in OP's answer but note this code is fetching the service without relying upon the element being bound to the app module "main.app".

Individuality answered 26/6, 2013 at 19:22 Comment(5)
Oh yes i really need this. Thanks, thumb up for you :DEsperanzaespial
A word of warning: for me this solution did not work 100% because my service tried to fetch a template defined in an ng-template element and failed to do so (did not find it). However the solution proposed by @DorCohen works perfectly in that case, too.Stormi
"angular.injector creates a new injector function, it does not return the one associated with the bootstrapped app." as stated here. I.e., it will inject new $rootScope for your service.Epidemiology
How to get Myservice if myservice inturns call other service?Ule
In my case, the factory returned a function (not a new function), and so it looked more like this: angular.injector(['ng', 'main.app']).get("MyService")()Example
P
21

For me it worked with:

angular.element(document.body).injector().get("MyService")

I got 'Unknown provider' error when tried this:

angular.injector(['ng', 'main.app']).get("MyService")

and as i am using jqLite, i can't do

angular.element('*[ng-app]')

because selectors are not supported by jqLite, but i got [Dor Cohen] idea. My directive ng-app is on my body tag, then i can use:

angular.element(document.body).injector().get("MyService")

or

angular.element(document).find('body').injector().get("MyService")
Percival answered 3/8, 2016 at 14:4 Comment(1)
The accepted answer didn't work for me but this one did. +1Remiss
C
16

Using the following line helps to execute my method from the angularjs service:

angular.element('*[ng-app]').injector().get("MyService").GetName ();
Clactonian answered 27/6, 2013 at 12:40 Comment(1)
This solution didnt work for me but the immediate below one did.Gittens
W
0

Here's a utility method that I use :

function getService(name, element) {
    element = element || '*[ng-app]';
    return angular.element(element).injector().get(name);
}

getSrv("name-of_service", document.body)

Witchery answered 17/10, 2016 at 19:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.