Last active
August 29, 2015 13:56
-
-
Save JasonOffutt/8961008 to your computer and use it in GitHub Desktop.
Inject env variable into Angular.js code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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