1

I'm developing an app based on mean.js boiler plate.

In one of my Angular controllers, I need to "know" whether the app is running in dev, test, or prod.

When the hosting Node app starts, this is indicated via the NODE_ENV environment variable, so Node knows where its running.

How do I pass this knowledge onto the Angular part of the app?

David Sykes
  • 48,469
  • 17
  • 71
  • 80
Eugene Goldberg
  • 14,286
  • 20
  • 94
  • 167

2 Answers2

1

If you have this knowledge on your backend, why not make an endpoint you can call in an AngularJS run method. While I do not know the details of your backend implementation, surely you can return a variable which represents this information. You can simply craft something like the following...

app.run(['$rootScope', '$http', function ($rootScope, $http) {
    $http.get('/yourEndoint').success(function(response) {
        $rootScope.whereAmI = response.whereAmI;
    });
}]);

Where wereAmI is some value you return from your backend and return to this call. If you place it on $rootScope, all other $scope's will inherit this value so you'll have knoweldege of "where you're at" app wide.

For more info on the run function, check out the AngularJS module docs

Run blocks are the closest thing in Angular to the main method. A run block is the code which needs to run to kickstart the application. It is executed after all of the services have been configured and the injector has been created. Run blocks typically contain code which is hard to unit-test, and for this reason should be declared in isolated modules, so that they can be ignored in the unit-tests.

scniro
  • 16,844
  • 8
  • 62
  • 106
1

In my case, I use gulp/grunt build task to select the requested config.js file (ie. production.config.js). Then you can use something like npm args to set an --env=production or --env=development variable when running your build task, which then is used by the gulp/grunt task to grab the requested config file.

Then your actual config file will just be an angular.constant(...) component with settings to use for your app.

ngDeveloper
  • 1,304
  • 2
  • 16
  • 34
  • Thank you very much for your suggestion! I picked the other one as an answer, as it fits a bit more naturally with the other elements of my flow (at least for now) – Eugene Goldberg Jun 04 '15 at 00:15