Grégoire Sion Grégoire Sion - 4 months ago 9
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

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'
});
Comments