skip skip - 8 months ago 79
AngularJS Question

How to pass data from EJS template to AngularJS controller

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


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


<%= user %>

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

Could somebody help me with it?

Answer Source

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:

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

Then it will be available like any other dependency.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download