title: Is it possible to define an angularjs constant inside a controller or config block?
body: <p>I am trying to set up some app wide constants from a http endpoint. I don't want (or need ) to do it via the manual bootstrap way (ala <a href="https://blog.mariusschulz.com/2014/10/22/asynchronously-bootstrapping-angularjs-applications-with-server-side-data" rel="nofollow">this</a> ). Ideally I want to load the constants in after the user has signed in. So I assume I need to define the constants somewhere I can run some code (and use $http). But whenever I define a constant inside a controller or a config block, the constant gives an unknown provider error when I pass it into another module. As soon as I move the definition outside of the controller it works. So for example, if I have ...</p>
  
  <pre><code>  var app = angular.module('testApp',[]);
        app.constant('test', 'test value');
  </code></pre>
  
  <p>then I can pass that into another module's controller like this:</p>
  
  <pre><code>  var app2 = angular.module('anotherModule',[testApp]);
    app2.controller('TestCtrl', ['test',
      function(test) {
          console.log(test)`
      }..
  </code></pre>
  
  <p>and this will output 'test value' as you would expect. But if the constant is defined inside a code block then it seems it doesn't register as a provider. So, for example:</p>
  
  <pre><code>  var app = angular.module('testApp',[]);
        app.controller('firstCtrl',function(){
                 app.constant('test', 'test value');
        }
  </code></pre>
  
  <p>If I run a page with that controller, the constant seems to register (in the sense that it is listed inside the _invokeQueue array on the testApp module) but the injector service doesn't have a provider for it and I get an unknown provider method for it on anotherModule. </p>
  
  <p>I initially felt that a factory or service was overkill for what I was trying to do but maybe that is the way I should go. But I would also love to understand why a constant defined like this isn't injectable.</p>