0

We are using Angular with Bower with a Python API for our stack. We have multiple environments that the code runs in - dev, staging, prod, etc. As of right now, we have to manually change connection strings for the front-end after we push fresh code to it to accommodate the environment it's in - but obviously that's not the right way to do things. To elaborate, say, we have following hosts for the front end: www.something.com and www.test-something.com and they should connect to apisomething.com and test-apisomething.com to pull data, respectively.

My objective is to be able to deploy code to any of the environments without having to change any values. Usually, I'd use environment variables on the server side to create all the links, but since Angular runs in a browser, those variables can't be accessed. So I am not sure how to proceed.

My research came up with tutorials using Grunt here and here but since we aren't using Grunt, that's not working out.

My thought is to write a Python script to manually generate a "constants.js" in the same directory as the app.js file resides (it would have to be run once for each environment) and include that file into git.ignore. The script will take a parameter and basically be a one-liner: "var base_url = \"www.something.com\"" where the value of base_url will vary with the parameter passed into the script. This is as far as I have gotten.

So, here are the questions:

  1. Is there a better way?
  2. If not, what is the best way to make the value of base_url accessible in the Angular app? Service, module, etc? And how would that work?
2
  • Why don't you simply use the $location service to know what the URL of the page is? docs.angularjs.org/api/ng/service/$location Commented Apr 9, 2015 at 19:14
  • This works. $location.host() does exactly what I need. Thank so much. Please add this as an answer and I will gladly accept it. Commented Apr 9, 2015 at 19:40

1 Answer 1

1

You just need to use the $location service to get information about the URL of the running application.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.