I want to store Url in configuration file so when I deployed this on Testing server or on Production I have to just change the url on config file not in js file but I don't know how to use configuration file in angular js
Asked
Active
Viewed 1,185 times
2 Answers
3
You can use angular.constant for configurations.
app.constant('appConfigurations', {
link_url: "http://localhost:8080/api",
//For production u can simply change the link_url
//link_url: "http://production_url/api"
});

Srigar
- 1,648
- 3
- 14
- 28
1
There are ways you can deal with it , but while coming to our implementation we used to do the following way
Create an External Environment js file
(function (window) { window.__env = window.__env || {}; window.__env.apiUrl = 'your Api Url'; }(this));
In your Index.html
add env.js above the app.js
<!-- Load environment variables -->
<script src="env.js"></script>
In your app.js
var envr ={}; if(window){ Object.assign(envr,window.__env) } // Define AngularJS application var app= angular.module('myapp', []);
// Register environment in AngularJS as constant app.constant('__env', env);
Update:
For adding additional URl in Config File:
(function (window) {
window.__env = window.__env || {};
window.__env.apiUrl = 'your Api Url';
//Another Url
window.__env.baseUrl ='/';
}(this));

Krsna Kishore
- 8,233
- 4
- 32
- 48
-
If i want to store different url in external enviorment file then how can i do it? – Leena Apr 11 '17 at 05:57
-
@Leena if it helps you please mark it as Answers along with the Upvote – Krsna Kishore Apr 11 '17 at 09:02