Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass an object to angularjs template from jade

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?

like image 411
Mozak Avatar asked Sep 10 '14 07:09

Mozak


1 Answers

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.

like image 63
jvrdelafuente Avatar answered Sep 21 '22 17:09

jvrdelafuente