Pass the object to the jade angularjs pattern

I am trying to pass an object from node to a client as shown below

render: function(req,res){ res.render('auth',{ userData : req.session.user }); } 

In my auth.jade file below code

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

So, I redirect the application to the new route that I defined in routeProvider using angularjs

 app.config(['$routeProvider','$locationProvider', function($routeProvider,$locationProvider) { $routeProvider. when('/profile', { templateUrl: 'templates/profile.html', controller: 'ProfileCtrl' }) 

So, is there any way I can access the data object in the controller for this route?

+5
source share
1 answer

You can do this in your script:

 var data = !{JSON.stringify(userData)}; window.serverData= data; 

In your app.js application 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 the var window in the controller without doing app.value, but this is good practice.

+7
source

Source: https://habr.com/ru/post/1202219/


All Articles