Skip to content

Instantly share code, notes, and snippets.

@JasonOffutt
Last active August 29, 2015 13:56
Show Gist options
  • Save JasonOffutt/8961008 to your computer and use it in GitHub Desktop.
Save JasonOffutt/8961008 to your computer and use it in GitHub Desktop.
Inject env variable into Angular.js code
<html>
<head>
<title>Some page</title>
<script src="path/to/angular.js"></script>
<script src="path/to/scripts.js"></script>
<script type="text/javascript">
// Setting a constant/value in Angualr allows it to be accessed via dependency injection
angualr.module('someApp').value('apiUrlBase', '<?php echo getenv('API_URL_BASE'); ?>');
</script>
</head>
<body>
<!-- ... -->
</body>
</html>
# Setting it as a defined value allows me to inject it into my services
angular.module('someApp')
.factory('UserService', ($http, apiUrlBase) ->
return {
fetchUsers: ->
promise = $http.get apiUrlBase + '/users'
promise.success (data) -> @users = data
return promise
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment