I am trying to pass an object from the node to the client like below
render: function(req,res){
    res.render('auth',{
        userData : req.session.user
    });
  }
In my auth.jade the code is as below
script.
    var data = !{JSON.stringify(userData)}
    console.log(data)
    window.top.location='/profile'
So I am redirecting the application to a new route which I have defined in the routeProvider using angularjs
app.config(['$routeProvider','$locationProvider',
  function($routeProvider,$locationProvider) {
    $routeProvider.
      when('/profile', {
        templateUrl: 'templates/profile.html',
        controller: 'ProfileCtrl'
      })
So is there a way by which I can access the 'data' object in the controller for that route?
You can do this into your script:
var data = !{JSON.stringify(userData)};
window.serverData= data;
After in your app.js, you can do this:
app.value('serverData', window.serverData);
And in your controller:
app.controller('controllerName', ['serverData', function(serverData){
console.log(serverData);
}]);
You can access to window var into the controller without doing app.value, but it is a good practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With