express angular ejs passing variable to template

不羁的心 提交于 2019-12-12 01:58:06

问题


I am trying to pass a message variable to my ejs template after login authentication:

app.post('/login', passport.authenticate('local-login', {
        successRedirect : '/profile', 
        failureRedirect : '/login', 
        failureFlash : true 
    }));

Then the code in the get block:

if(req.isAuthenticated()) res.render('partials/' + name, { message: 'test' });

So if login fails the user will be notified. the login.ejs partial will show the message, but message does not show up in the template:

<script>var message = "<%=message%>";</script>

<div class="col-sm-6 col-sm-offset-3">

    <h1 class="form-fonts"><span class="fa fa-sign-in"></span>Login</h1>

    <!-- show any messages that come back with authentication -->
    <div ng-if="message.length > 0" class="alert alert-danger">{{message}}</div>

However, when I can see the logged message in the controller:

app.controller('LoginCtrl', ['$scope', '$http', 
  function($scope, $http) {
    console.log('message:  ' + message);   // outputs message:  test
}]);

回答1:


If you want your JSON unescaped you would use a -;

<script>var message="<%-message%>";</script>

but if it's not templating at all you have other issues. You may you need to use JSON.stringify(obj).



来源:https://stackoverflow.com/questions/24976338/express-angular-ejs-passing-variable-to-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!