-2

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

Leena
  • 9
  • 2
  • 1
    Possible duplicate of [Configuration file in AngularJS](http://stackoverflow.com/questions/17876439/configuration-file-in-angularjs) – rudolf_franek Apr 10 '17 at 14:35

2 Answers2

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

  1. Create an External Environment js file

    (function (window) {
      window.__env = window.__env || {};
    
      window.__env.apiUrl = 'your Api Url';
    
    }(this));
    

  2. In your Index.html

add env.js above the app.js

<!-- Load environment variables -->
<script src="env.js"></script>
  1. 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