Narek Mamikonyan Narek Mamikonyan - 1 month ago 14
AngularJS Question

Use Angular constant outside of angular

I know it is not a good practice to use Angular services outside of angular but nevertheless it's very interesting for me, for example I have

.constant('APIprefix','/api')


how can I retrieve APIprefix value outside of angular scope? For instance from other js file which is not in angular scope.

Answer

You can access any service like this:

angular.element(document.documentElement).injector().get('APIprefix');

Note that you should pass to angular.element DOM node where you put ng-app. In the example above document.documentElement is HTML tag.

Demo: http://plnkr.co/edit/nf8zhDsl1PAnE5zDYYaG?p=preview

Comments