2

I have an AngularJS application, which is supposed to call several Spring-based REST API's, url of which would be dynamic based on the environment. Like if I am running on dev environment, REST URL would be localhost:7777/.. If on production, it would be productionserver:7777/....

Now I have to provide these API URLs to Angular. What is the best way to configure these REST URLs at run time in my AngularJS application?

1

4 Answers 4

0

You could place all of your api configuration into a single json file named for example config.json, and then before application is bootstrapped you can fetch that data and make a constant out of it and after inject it anywhere you need, something like the following:

Lets say config.json contains these values:

{
 "ApiUrls": {
   "ApiUrl1": "value1",
   "ApiUrl2": "value2",
   "ApiUrl3": "value3"
  }
}

Injecting configuration api urls before bootstrapping:

(function () {
fetchData().then(bootstrapApp);

function fetchData() {
    var initInjector = angular.injector(["ng"]);
    var $http = initInjector.get("$http");

    return $http.get("config.json").then(function (response) {
        app.constant("ApiConfiguration", response.data.ApiUrls);
    }, function (errorResponse) {
        alert(errorResponse);
        // Handle error case
    });
}

function bootstrapApp() {
    angular.element(document).ready(function () {
        angular.bootstrap(document, ["app"]);
    });
}
}());

and for example your service:

app.factory('factory', factory);

factory.$inject = ['ApiConfiguration'];

function factory(ApiConfiguration){
   var serviceFactory = {
     ApiUrl1: ApiConfiguration.ApiUrl1
   }
   return serviceFactory;
}

This way you can set your values inside the config.json file as per enviroment. Locally it will be localhost:7777/.., in production it will be productionserver:7777/... etc..

Sign up to request clarification or add additional context in comments.

Comments

0

Create Service for rest urls or REST Data:-

 (function () {
var injectParams = ['$location'];
var service = function ($location) {
    var vm = this;
    vm.baseUrl = 'http://' + $location.host() +':'+$location.port() + '/api/v1/';
    return vm;
};
//bootstrapping
service.$inject = injectParams;
angular.module('showdown').service('restURL', service);
}());

Comments

0

Depending on your setup, you may want to inject your URLs via ng-init. Just make sure to JS and HTML escape the string, if necessary.

So your HTML would look something like this:

<div ng-app="myApp" ng-init="
    myUrl='${myUrl?html?js_string}';
    myUrl2='${myUrl2?html?js_string}';">

And your Javascript would look something like this:

angular.controller("myApp", () => ($scope) => {
    $scope.myUrl; //url is here
});

Comments

-1

Create a const in ur app like SERVER-ENV and write a build task to change the value of this constant based on the server environment. Now you can create URL using this const e.g. SERVER-ENV + ""; This task can be automated with gulp or grunt.

1 Comment

const SERVER-ENV;? You'll need to be more specific and provide a piece of code in your answer. Also, OP does not mention any possibility of gulp or grunt in the solution. It asks run time, which excludes building tools.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.