问题
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