title: Set Angularjs constants at project startup from $http.GET
body: <p>In my application I have an orchestration service that gets the uris from different services that register with it. <code>service_1</code> and <code>service_2</code> could be on different machines but one registered, the uris of their machines will be stored.</p>
  
  <p>In my other application which makes use of that orchestration service, I want to call to the orchestration service to get the uris to use, but then I want to set them as Angular constants, or at least be able to use the uri's values.</p>
  
  <p>So this is the service that's going to be using the 'constant' which is the uri pulled from orchestration service:</p>
  
  <pre><code>(function () {
      'use strict';
  
      angular
          .module('data.model-view', ['restapi'])
          .factory('MVService', MVService);
  
      MVService.$inject = ['$http', '$q', 'exception', 'logger', 'restapi'];
      /* @ngInject */
      function MVService($http, $q, exception, logger, restapi) {
  
          var HOST = restapi.mvservice.HOST;
          var MODULE = restapi.mvservice.MODULE;
      ...
      //below is an example of what will use the above host/module in order to 
      //get the data needed for this application
  
      function getModels() {
          return $http.get(HOST + MODULE + '/model/retrieveAll')
              .then(success)
              .catch(fail);
  
          function success(response) {
              return response.data;
          }
          function fail(e) {
              return exception.catcher('XHR Failed for retrieveAll')(e);
          }
      }
  </code></pre>
  
  <p>So then this is <code>restapi</code> module where I'd like the constants to be set, so I can have access to them throughout the application, but I need to get them from the orchestration service first.</p>
  
  <pre><code>(function() {
      'use strict';
      var data = '';
      angular
          .module('restapi', [])
          .factory('restapi', function($http, exception) {
              var HOST = %%ORCSERVICE%%;
              var MODULE = '/orchestration/service/rest/data';
  
              return $http.get(HOST + MODULE)
              .then(success)
              .catch(fail);
  
              function success(response) {
                  //so at this point I can actually access the data I need
                  //with a console.debug(response.data);
                  return response.data;
              }
  
              function fail(e) {
                  return exception.catcher('XHR Failed to reach orc')(e);
              }
          }).constant('restapi', constant());
  
      function constant() {
          //set constants here based on the data pulled from above
  
         //ideally I want the result of this to be like
         //{
         //    mvservice: {
         //        'HOST': 'http://xxxxxxxxxx.amazonaws.com'
         //        'MODULE': '/rest/service/whatever'
         //    },
         //    ... //other service here
         //}
      }
  
  })();
  </code></pre>
  
  <p>Like I say in the comment above, I can actually get the data I need (the uris) from the <code>$http.get</code> immediately above. I'd just like then to be able to get the data set as a constant, or at least in a form that I can access it. Because when <code>MVService</code> spins up, it needs the its own uir from the orchestration service in order to be able to make its rest calls. Sorry might be a little confusing, let me know if there is a need for clarification.</p>