简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM