Reputation: 10412
Is there a way we can access AngularJS constant in normal Javascript function in a different file?
I have example below:
angular.module('app').constant('env', {
url: 'http://localhost:1337/'
});
How do I access env constant in Javascript function?
Upvotes: 1
Views: 459
Reputation: 4302
Yes. If you had an element the app was initialised on:
<div id="app" ng-app="yourApp"></div>
Get an injector using the angular function:
var inj = angular.element(document.getElementById('app')).injector();
var constant = inj.get('myConstant'); // in your case, env
Upvotes: 1