传递一个对象从玉angularjs模板对象、模板、angularjs

2023-09-13 03:23:08 作者:情圣 情似入心㎜°

我试图从节点传递一个对象给客户端像下面

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
    });
  }

在我auth.jade的code是如下

In my auth.jade the code is as below

script.
    var data = !{JSON.stringify(userData)}
    console.log(data)
    window.top.location='/profile'

所以我重定向应用程序,我已经在使用routeProvider定义angularjs一条新的路线

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;

在您的app.js后,你可以这样做:

After in your app.js, you can do this:

app.value('serverData', window.serverData);

和在你的控制器:

app.controller('controllerName', ['serverData', function(serverData){

console.log(serverData);

}]);

您可以访问窗口VAR到控制器而不做app.value,但它是一个很好的做法。

You can access to window var into the controller without doing app.value, but it is a good practice.