Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular js read environmental variables

I am trying to read environment variables in http get calls

$http.get('http://'  + $location.host() + ':8080/api')

I want to be able to read the environmen variable and use it as the http rest server in teh above API call, as follows

 $http.get('environmental_variable ':8080/api')

Note: I dont know the environment variable until runtime So I cannot have the value before hand to use it as a constant

like image 263
user_mda Avatar asked Oct 30 '15 20:10

user_mda


People also ask

Can Angular read environment variables?

Angular provides build-in support to configure and manage Environment Variables. It keeps the environment configuration under the folder src/environments folder.

Where is .env file in Angular?

In Angular, we have our environment. ts and environment. prod. ts files defined in the src/environments folder.


1 Answers

There are lots of examples showing how you can put your settings into different files or constants. Most of these work, but miss the point.

Your configuration settings are not part of your code!

Apart from the 'Hello World' examples, your deployment should be carried out by a CI/CD server and this should be responsible for setting your configuration settings. This has a number of benefits:

1) You are deploying the same code to different environments. If you deploy code to a test environment, then you want to deploy the same code to your production environment. If your servers have to rebuild the code, to add the production configuration settings, you are deploying different code.

2) Code can be shared without giving away your API details, AWS settings and other secret information.

3) It allows new environments to be added easily.

There are lots of examples out there on how to do this. One example is www.jvandemo.com/how-to-configure-your-angularjs-application-using-environment-variables

like image 156
Karl Gjertsen Avatar answered Nov 06 '22 18:11

Karl Gjertsen