Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Factory From Outside of AngularJS

How do you call a factory? As defined below.

angular.module('fb.services', []).factory('getQueryString', function () {
    return {
        call: function () {
            var result = {}, queryString = qs.substring(1),
                re = /([^&=]+)=([^&]*)/g,
                m;
            while (m = re.exec(queryString))
            result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
            return result;
        }
    }
});
alert(getQueryString.call('this=that&me=you'));
like image 464
Kohjah Breese Avatar asked Dec 06 '22 00:12

Kohjah Breese


1 Answers

If you want to call your factory outside of the angular you would need to get an injector from your module. i.e:

angular.injector(['fb.services']).get('getQueryString').call();

You can typically use this while writing unit test, but you should try to avoid doing this in production code.

Try not to access angular app outside, Otherwise typical usage of the factory/services etc would be through dependency injection while you are in the app.

like image 149
PSL Avatar answered Dec 22 '22 10:12

PSL