3

I've got the user data from ExpressJS through NodeJS as following:

app.js

app.get('/user', function(req, res) {
    res.render('users', { title: 'Foo', user: req.user });
});

users.ejs

<%= user %>

I need the user data from user.ejs above to an angularjs controller, so I could display that on other named views.

Could somebody help me with it?

skip
  • 12,193
  • 32
  • 113
  • 153
  • 1
    see http://www.mircozeiss.com/how-to-pass-javascript-variables-from-a-server-to-angular/ & http://stackoverflow.com/questions/21399482/expressjs-variables-in-angularjs-mean-stack – laggingreflex Oct 19 '14 at 08:34

2 Answers2

6

Some say ng-init but it just feels dirty and wrong to me. You can put it inline and mess with the window and rootscope but it can get messy. Or you can inline another module like:

<script>
   angular.module('PreloadedData', [])
     .constant('User', <%- user %>);
</script>

Then it will be available like any other dependency.

Dylan
  • 4,703
  • 1
  • 20
  • 23
0
try this on template...

<% if(title){ %>
 <script>
window.Mytitle = <%- JSON.stringify(title) %>;
</sript>
<% } %>

try on your controller..
console.log(window.Mytitle);

================================================

try this on template...

<% if(user){ %>
<script>
  window.MyUser = {};
  window.MyUser = <%- JSON.stringify(user) %>;
</sript>
<% } %>

try on your controller..
console.log(window.MyUser);

sorry if am wrong..

rk.
  • 149
  • 1
  • 4