Visual Studio - Javascript Project define global variable with different publishings

378 views Asked by At

Is there a way, in a Project that only uses JavaScript (AngularJs) and HTML to assign a global variable used on the controllers.js based on the Publish Profile?

For instance, in a Project of C# I can define a value on the Web.Config->AppSettings and the value changes according to the publish I make (Development, Quality, Production), using the Web.Config.Transform

Since I can't have access to the web.config file of the Javascript Project, is there anyway to me to accomplish this? The value that I need to change is the url to the API, which is different for any environment (Development,Quality,Production).

Presently it's hard coded, like this:

   controllers.js

//var baseUrl = "http://something.azurewebsites.net/"; //Quality
var baseUrl = "http://localhost:1050/"; //Development

myApp.controller('loginController',.......

the baseUrl is then used on every controller to acces the API

1

There are 1 answers

0
NunoRibeiro On BEST ANSWER

So, after many searches and realizing I can't access the Web.config file, I come up with this resolution:

var baseUrl = checkBaseUrl(window.location.host);

function checkBaseUrl(host)
{
  // in production
  if (host.indexOf("www.domain.com") > -1)
     return "https://www.domain.com/";

  // in localhost
  if (host.indexOf("localhost") > -1)
    return "http://localhost:1134/";

  // in develop
  if (host.indexOf("-dev") > -1)
    return "https://name-dev.domain.com/";

  // in tests
  if (host.indexOf("-test") > -1)
    return "https://name-test.domain.com/";

  // in staging
  return "https://name-staging.domain.com/"
}

And this does the trick