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?
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.