Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from EJS template to AngularJS controller

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?

like image 676
skip Avatar asked Feb 17 '26 03:02

skip


1 Answers

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.

like image 126
Dylan Avatar answered Feb 19 '26 16:02

Dylan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!