Pass an object to angularjs template from jade

隐身守侯 提交于 2019-12-06 03:33:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25759927/pass-an-object-to-angularjs-template-from-jade

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!