<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script data-require="angularjs@1.5.8" data-semver="1.5.8" src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js"></script>
<!-- Load AngularJS application -->
<script src="script.js"></script>
<!-- Load environment variables -->
<script src="env.js"></script>
</head>
<body>
<h1>Using Angular 1.x with ENV vars</h1>
<p>Please <strong>Run</strong> check the <strong>console</strong> output</p>
</body>
</html>
// Code goes here
'use strict';
disableLogging.$inject = ['$logProvider', 'ENV'];
// app config
function disableLogging($logProvider, ENV) {
$logProvider.debugEnabled(ENV.ENABLEDEBUG);
}
angular.module('myApp', [])
.config(disableLogging)
.config(function(ENV) {
console.info('oh hi ENV', ENV);
});
# Using Angular 1.x with ENV vars
## Background
Inspired by "
How to configure your AngularJS application using environment variables" by https://twitter.com/jvandemo
http://www.jvandemo.com/how-to-configure-your-angularjs-application-using-environment-variables
## This approach
Instead of using the global `window` object use an Angular constant.
'use strict';
(function() {
angular.module('myApp')
.constant('ENV', {
// API url
APIURL: 'http://yoururl.com:8080',
// Base url
BASEURL: '/',
// Whether or not to enable debug mode
// Setting this to false will disable console output
ENABLEDEBUG: true
});
})();