Grégoire Sion Grégoire Sion - 1 year ago 65
AngularJS Question

store url in conf file to be used as global variable

My app curently has several controllers and each one of them uses an url to do request for the view it works for.

My url look like this : htttp://myurl/requestSpecificHeader.

Url is currently hard coded which can't do. I would like it to be stored in a config.txt or .json or something easy to rewrite, than get it to my app.js and make it a global variable to be used in one controller. As my app is fairly small (4 views) I only have one file that hold all my controllers and services.

What would be the best way to proceed ?

Answer Source

Declare it as a constant:

angular.module('App').constant('myUrl', 'http://myurl/');

Now you can inject this value into your controllers:

app.controller('Ctrl', function(myUrl){
    var thisUrl = myUrl + 'requestSpecificHeader'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download