javascript - how to pass a value from node/express app to an AngularJS controller -
i'm developing app based on mean.js boiler plate.
in 1 of angular controllers, need "know" whether app running in dev, test, or prod.
when hosting node app starts, indicated via node_env environment variable, node knows running.
how pass knowledge onto angular part of app?
if have knowledge on backend, why not make endpoint can call in angularjs run
method. while not know details of backend implementation, surely can return variable represents information. can craft following...
app.run(['$rootscope', '$http', function ($rootscope, $http) { $http.get('/yourendoint').success(function(response) { $rootscope.whereami = response.whereami; }); }]);
where wereami
value return backend , return call. if place on $rootscope
, other $scope
's inherit value you'll have knoweldege of "where you're at" app wide.
for more info on run
function, check out angularjs module docs
run blocks closest thing in angular main method. run block code needs run kickstart application. executed after of services have been configured , injector has been created. run blocks typically contain code hard unit-test, , reason should declared in isolated modules, can ignored in unit-tests.
Comments
Post a Comment