Passionate Engineer
Passionate Engineer

Reputation: 10412

How to access angularjs constant from regular Javascript function

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

Answers (1)

Peter Ashwell
Peter Ashwell

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

Related Questions