[英]Access connect-flash messages in angular.js
我正在修改一些代码,这些代码以传统的非SPA页面加载为前提,并向用户显示由connect-flash中间件生成的flash消息。 它通过传入在EJS文件中呈现的对象来实现。
我的应用程序是有角度的,后端没有模板引擎,因此我想避免使用它。 我如何才能对这些即显信息有所了解?
这里是上下文:passportjs会在错误时重定向到/ signup
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
谢谢你的帮助!
我的解决方案是创建一个EJS页面,其唯一目的是提取connect-flash数据并将浏览器重定向到指定的路由,并将数据附加为querystring。
// process the signup form
app.post('/signup', passport.authenticate('local-signup', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
app.get('/signup', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('redirect.ejs', {redirect_url: "/?route=/signup&message="+req.flash('signupMessage')});
});
redirect.ejs
<html>
<meta http-equiv="refresh" content="0;<% if (redirect_url) { %><%=redirect_url %><% } %>" />
</html>
角度控制器
app.controller("Index", function ($scope, $route, $routeParams,$location) {
var route= $routeParams.route, msg=$routeParams.message;
if (route) {
if ($location.$$search.route) {
delete $location.$$search.route;
$location.$$compose();
}
console.log(route, msg);
$location.path(route);
}
});
它本来会更糟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.