1

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

How am I able to access angular object in regular Gulp task js file?
You could have mentioned gulp in your question
No worries maybe I should have been more descriptive. How do I do this in Gulp?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.